First, set the working directory:
setwd("~/Teaching/Statistik 3/Uebungen/Uebung 3")
load the data:
load("../beispieldaten.RData")
# data = na.omit(beispieldaten)
create variables:
leben <- beispieldaten$leben_gesamt
unterstuetzung <- beispieldaten$unt_eltern
harmonie <- beispieldaten$swk_sozharm
and create centred variables:
# unterstuetzungCentred <- unterstuetzung - mean(unterstuetzung,
# na.rm=TRUE)
unterstuetzungCentred <- scale(unterstuetzung, center = TRUE, scale = FALSE)
# harmonieCentred <- harmonie - mean(harmonie, na.rm=TRUE)
harmonieCentred <- scale(harmonie, center = TRUE, scale = FALSE)
create interaction variables:
interaction <- unterstuetzung * harmonie
interactionCentred <- unterstuetzungCentred * harmonieCentred
first, create the model without interaction:
model1 <- lm(leben ~ unterstuetzung + harmonie)
summary(model1)
##
## Call:
## lm(formula = leben ~ unterstuetzung + harmonie)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.6939 -0.3199 0.0072 0.3948 1.0912
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.8561 0.2512 7.39 1.7e-12 ***
## unterstuetzung 0.3212 0.0323 9.95 < 2e-16 ***
## harmonie 0.3172 0.0411 7.72 2.1e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.525 on 281 degrees of freedom
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.429, Adjusted R-squared: 0.425
## F-statistic: 106 on 2 and 281 DF, p-value: <2e-16
create the interaction model using lm() with formula notation:
model2 <- lm(leben ~ unterstuetzung + harmonie + interaction)
summary(model2)
##
## Call:
## lm(formula = leben ~ unterstuetzung + harmonie + interaction)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.6610 -0.3332 0.0091 0.3959 1.0944
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.4813 0.9213 -0.52 0.6018
## unterstuetzung 0.7418 0.1628 4.56 7.8e-06 ***
## harmonie 0.7731 0.1778 4.35 1.9e-05 ***
## interaction -0.0814 0.0309 -2.63 0.0089 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.52 on 280 degrees of freedom
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.443, Adjusted R-squared: 0.437
## F-statistic: 74.2 on 3 and 280 DF, p-value: <2e-16
next, do the same with the centred variables:
modelCentred1 <- lm(leben ~ unterstuetzungCentred + harmonieCentred)
summary(modelCentred1)
##
## Call:
## lm(formula = leben ~ unterstuetzungCentred + harmonieCentred)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.6939 -0.3199 0.0072 0.3948 1.0912
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.4169 0.0312 173.88 < 2e-16 ***
## unterstuetzungCentred 0.3212 0.0323 9.95 < 2e-16 ***
## harmonieCentred 0.3172 0.0411 7.72 2.1e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.525 on 281 degrees of freedom
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.429, Adjusted R-squared: 0.425
## F-statistic: 106 on 2 and 281 DF, p-value: <2e-16
create the interaction model using lm() with formula notation:
modelCentred2 <- lm(leben ~ unterstuetzungCentred + harmonieCentred + interactionCentred)
summary(modelCentred2)
##
## Call:
## lm(formula = leben ~ unterstuetzungCentred + harmonieCentred +
## interactionCentred)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.6610 -0.3332 0.0091 0.3959 1.0944
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.4331 0.0314 172.85 < 2e-16 ***
## unterstuetzungCentred 0.3121 0.0321 9.71 < 2e-16 ***
## harmonieCentred 0.2953 0.0415 7.11 9.5e-12 ***
## interactionCentred -0.0814 0.0309 -2.63 0.0089 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.52 on 280 degrees of freedom
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.443, Adjusted R-squared: 0.437
## F-statistic: 74.2 on 3 and 280 DF, p-value: <2e-16
anova(model1, model2)
## Analysis of Variance Table
##
## Model 1: leben ~ unterstuetzung + harmonie
## Model 2: leben ~ unterstuetzung + harmonie + interaction
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 281 77.5
## 2 280 75.6 1 1.87 6.94 0.0089 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(modelCentred1, modelCentred2)
## Analysis of Variance Table
##
## Model 1: leben ~ unterstuetzungCentred + harmonieCentred
## Model 2: leben ~ unterstuetzungCentred + harmonieCentred + interactionCentred
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 281 77.5
## 2 280 75.6 1 1.87 6.94 0.0089 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
create variables:
harmonie_sdo <- harmonie - (mean(harmonie) + sd(harmonie))
harmonie_sdu <- harmonie - (mean(harmonie) - sd(harmonie))
interaction_sdo <- unterstuetzungCentred * harmonie_sdo
interaction_sdu <- unterstuetzungCentred * harmonie_sdu
create models:
modelSDU <- lm(leben ~ unterstuetzungCentred + harmonie_sdu + interaction_sdu)
summary(modelSDU)
##
## Call:
## lm(formula = leben ~ unterstuetzungCentred + harmonie_sdu + interaction_sdu)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.6610 -0.3332 0.0091 0.3959 1.0944
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.2016 0.0461 112.71 < 2e-16 ***
## unterstuetzungCentred 0.3759 0.0381 9.87 < 2e-16 ***
## harmonie_sdu 0.2953 0.0415 7.11 9.5e-12 ***
## interaction_sdu -0.0814 0.0309 -2.63 0.0089 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.52 on 280 degrees of freedom
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.443, Adjusted R-squared: 0.437
## F-statistic: 74.2 on 3 and 280 DF, p-value: <2e-16
modelSDO <- lm(leben ~ unterstuetzungCentred + harmonie_sdo + interaction_sdo)
summary(modelSDO)
##
## Call:
## lm(formula = leben ~ unterstuetzungCentred + harmonie_sdo + interaction_sdo)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.6610 -0.3332 0.0091 0.3959 1.0944
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.6646 0.0443 127.82 < 2e-16 ***
## unterstuetzungCentred 0.2483 0.0423 5.87 1.2e-08 ***
## harmonie_sdo 0.2953 0.0415 7.11 9.5e-12 ***
## interaction_sdo -0.0814 0.0309 -2.63 0.0089 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.52 on 280 degrees of freedom
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.443, Adjusted R-squared: 0.437
## F-statistic: 74.2 on 3 and 280 DF, p-value: <2e-16