2.3 At-Home Exercises
2.3.1 Setup
To complete the following exercises, you’ll need to install the packages described in the following table.
| Package | Description |
|---|---|
| haven | Loading data from SPSS ‘.sav’ files |
| tidySEM | Plotting and tabulating the output of SEM-models |
Use the install.packages() function to install these packages.
Click for explanation
- Open RStudio
- Inside RStudio, find the window named Console on left side of the screen.
- Copy the following code into the console and hit
Enter/Returnto run the command.
2.3.2
Use the haven::read_spss() function to load the LifeSat.sav data.
2.3.3
Make a table of descriptive statistics for the variables: LifSat, educ, ChildSup, SpouSup, and age.
- What is the average age in the sample?
- What is the range (youngest and oldest child)?
Hint: Use the tidySEM::descriptives() function.`
Click for explanation
- The package
tidySEMcontains thedescriptives()function for computing descriptive statistics. - The
describe()function in thepsychpackage is a good alternative.
2.3.4
Run a simple linear regression with LifSat as the dependent variable and educ as the independent variable.
Hints:
- The
lm()function (short for linear model) does linear regression. - The
summary()function provides relevant summary statistics for the model. - It can be helpful to store the results of your analysis in an object.
Click for explanation
##
## Call:
## lm(formula = LifSat ~ educ, data = LifeSat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -43.781 -11.866 2.018 12.418 43.018
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 35.184 7.874 4.469 2.15e-05 ***
## educ 3.466 1.173 2.956 0.00392 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 17.64 on 96 degrees of freedom
## Multiple R-squared: 0.08344, Adjusted R-squared: 0.0739
## F-statistic: 8.74 on 1 and 96 DF, p-value: 0.003918
2.3.5
Repeat the analysis from 2.3.4 with age as the independent variable.
Click for explanation
##
## Call:
## lm(formula = LifSat ~ age, data = LifeSat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -35.321 -14.184 3.192 13.593 40.626
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 200.2302 52.1385 3.840 0.00022 ***
## age -2.0265 0.7417 -2.732 0.00749 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 17.75 on 96 degrees of freedom
## Multiple R-squared: 0.07215, Adjusted R-squared: 0.06249
## F-statistic: 7.465 on 1 and 96 DF, p-value: 0.007487
2.3.6
Repeat the analysis from 2.3.4 and 2.3.5 with ChildSup as the independent variable.
Click for explanation
##
## Call:
## lm(formula = LifSat ~ ChildSup, data = LifeSat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -37.32 -12.14 0.66 12.41 44.68
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 37.559 8.342 4.502 1.89e-05 ***
## ChildSup 2.960 1.188 2.492 0.0144 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 17.86 on 96 degrees of freedom
## Multiple R-squared: 0.06076, Adjusted R-squared: 0.05098
## F-statistic: 6.211 on 1 and 96 DF, p-value: 0.01441
2.3.7
Run a multiple linear regression with LifSat as the dependent variable and educ, age, and ChildSup as the
independent variables.
Hint: You can use the + sign to add multiple variables to the right hand side (RHS) of your model formula.
Click for explanation
##
## Call:
## lm(formula = LifSat ~ educ + age + ChildSup, data = LifeSat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -32.98 -12.56 2.68 11.03 41.91
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 134.9801 53.2798 2.533 0.0130 *
## educ 2.8171 1.1436 2.463 0.0156 *
## age -1.5952 0.7188 -2.219 0.0289 *
## ChildSup 2.4092 1.1361 2.121 0.0366 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 16.92 on 94 degrees of freedom
## Multiple R-squared: 0.1741, Adjusted R-squared: 0.1477
## F-statistic: 6.603 on 3 and 94 DF, p-value: 0.0004254