# Install package
library(lavaan)
## This is lavaan 0.6-16
## lavaan is FREE software! Please report any bugs.
# Reading in raw data:
read.delim("IncomeRegress_raw.txt", header=FALSE)
## V1 V2 V3
## 1 80.00 16 55
## 2 80.00 16 51
## 3 21.25 12 36
## 4 6.50 14 29
## 5 32.50 14 34
## 6 55.00 13 34
## 7 55.00 16 50
## 8 45.00 14 61
## 9 55.00 14 45
## 10 5.50 14 17
## 11 23.75 12 39
## 12 18.75 12 30
## 13 13.75 14 20
## 14 13.75 4 45
## 15 32.50 12 37
## 16 23.75 12 32
## 17 45.00 13 40
## 18 23.75 15 40
## 19 55.00 18 50
## 20 80.00 12 46
## 21 55.00 18 50
## 22 80.00 16 50
## 23 32.50 15 50
## 24 27.50 12 35
## 25 27.50 12 38
## 26 18.75 11 36
## 27 37.50 18 47
## 28 45.00 13 28
## 29 18.75 12 50
## 30 27.50 17 49
## 31 37.50 12 33
## 32 55.00 12 50
## 33 80.00 18 50
## 34 45.00 19 43
## 35 80.00 16 50
## 36 5.50 15 38
## 37 80.00 19 76
## 38 37.50 18 51
## 39 45.00 16 74
## 40 21.25 12 37
## 41 37.50 18 51
## 42 5.50 9 23
## 43 80.00 12 34
## 44 45.00 17 57
## 45 45.00 17 44
## 46 4.50 12 22
## 47 18.75 11 50
## 48 27.50 12 50
## 49 37.50 14 50
## 50 37.50 12 17
## 51 18.75 18 76
## 52 27.50 16 41
## 53 45.00 17 50
## 54 37.50 14 36
## 55 23.75 16 34
## 56 21.25 10 37
## 57 18.75 12 29
## 58 32.50 13 36
## 59 45.00 19 63
## 60 37.50 20 78
## 61 21.25 13 42
## 62 27.50 16 51
## 63 80.00 18 72
## 64 55.00 15 54
## 65 32.50 16 34
## 66 45.00 12 48
## 67 13.75 11 22
## 68 32.50 14 68
## 69 23.75 3 32
## 70 13.75 13 32
## 71 2.00 12 28
## 72 27.50 16 45
## 73 6.50 14 48
## 74 27.50 16 23
## 75 16.25 16 45
## 76 80.00 20 74
## 77 27.50 16 62
## 78 37.50 14 50
## 79 80.00 12 50
## 80 2.00 16 44
## 81 45.00 17 34
## 82 45.00 13 32
## 83 32.50 13 78
## 84 23.75 19 82
## 85 3.50 13 17
## 86 32.50 12 57
## 87 18.75 13 48
## 88 16.25 15 57
## 89 27.50 14 48
## 90 16.25 17 58
## 91 45.00 16 69
## 92 18.75 13 17
## 93 27.50 12 56
## 94 23.75 16 51
## 95 45.00 12 35
## 96 45.00 16 41
## 97 37.50 14 44
## 98 37.50 12 49
## 99 18.75 9 22
## 100 18.75 13 33
## 101 13.75 16 34
## 102 32.50 16 50
## 103 21.25 14 35
## 104 45.00 12 39
## 105 80.00 16 50
## 106 37.50 13 50
## 107 45.00 16 51
## 108 32.50 16 71
## 109 27.50 12 50
## 110 55.00 12 50
## 111 45.00 16 50
## 112 37.50 15 51
## 113 80.00 15 71
## 114 18.75 14 50
## 115 21.25 11 18
## 116 37.50 16 68
## 117 32.50 16 57
## 118 27.50 19 63
## 119 45.00 14 50
## 120 13.75 14 22
## 121 27.50 12 48
## 122 32.50 16 34
## 123 45.00 14 51
## 124 55.00 14 48
## 125 21.25 15 50
## 126 80.00 16 34
## 127 16.25 9 41
## 128 55.00 17 78
## 129 16.25 14 17
## 130 45.00 10 45
## 131 27.50 10 33
## 132 37.50 12 42
## 133 23.75 12 35
## 134 37.50 17 71
## 135 80.00 18 76
## 136 45.00 12 30
## 137 37.50 20 76
## 138 37.50 12 32
## 139 37.50 11 36
## 140 18.75 12 41
## 141 32.50 16 50
## 142 55.00 19 76
## 143 55.00 20 62
## 144 45.00 17 50
## 145 45.00 12 22
## 146 27.50 18 68
## 147 18.75 15 17
## 148 23.75 20 47
## 149 27.50 20 82
## 150 16.25 12 48
## 151 13.75 12 47
## 152 27.50 16 47
## 153 37.50 12 22
## 154 32.50 19 61
## 155 37.50 15 48
## 156 21.25 16 61
## 157 45.00 12 50
## 158 45.00 14 43
## 159 23.75 20 78
## 160 32.50 14 50
## 161 32.50 17 14
## 162 18.75 14 39
## 163 13.75 13 50
## 164 27.50 13 37
## 165 18.75 17 46
## 166 5.50 12 32
## 167 37.50 13 51
## 168 13.75 12 17
## 169 80.00 15 50
## 170 16.25 7 40
## 171 18.75 11 35
## 172 21.25 12 36
## 173 6.50 12 31
## 174 11.25 16 20
## 175 9.00 10 23
## 176 9.00 15 36
## 177 21.25 8 29
## 178 32.50 17 32
## 179 13.75 9 22
## 180 27.50 12 49
## 181 18.75 14 50
## 182 23.75 12 39
## 183 37.50 12 33
## 184 13.75 16 34
## 185 45.00 12 35
## 186 55.00 17 67
## 187 11.25 12 24
## 188 21.25 12 48
## 189 13.75 12 48
## 190 21.25 12 24
## 191 27.50 12 40
## 192 45.00 17 68
## 193 16.25 16 27
## 194 80.00 13 50
## 195 18.75 12 45
## 196 0.50 10 30
## 197 55.00 14 34
## 198 32.50 8 17
## 199 27.50 12 48
## 200 13.75 6 23
## 201 13.75 13 48
## 202 37.50 12 27
## 203 2.00 7 22
## 204 32.50 12 40
## 205 37.50 14 50
## 206 21.25 12 50
## 207 37.50 14 50
## 208 27.50 12 32
## 209 9.00 14 16
## 210 13.75 4 17
## 211 13.75 16 34
## 212 27.50 16 61
## 213 32.50 12 50
## 214 3.50 11 17
## 215 23.75 13 45
## 216 18.75 12 23
## 217 80.00 16 50
## 218 11.25 16 56
## 219 37.50 12 51
## 220 9.00 12 33
## 221 80.00 12 36
## 222 23.75 13 28
## 223 23.75 20 61
## 224 27.50 14 47
## 225 11.25 12 22
## 226 23.75 12 40
## 227 21.25 11 30
## 228 32.50 16 50
## 229 55.00 20 47
## 230 45.00 14 50
## 231 32.50 20 63
## 232 80.00 16 22
## 233 55.00 20 76
## 234 13.75 17 50
## 235 23.75 17 50
## 236 27.50 12 49
## 237 18.75 16 50
## 238 80.00 12 57
## 239 27.50 14 23
## 240 5.50 14 50
## 241 4.50 12 47
## 242 27.50 14 37
## 243 16.25 12 72
## 244 45.00 17 36
## 245 45.00 15 49
## 246 23.75 16 72
## 247 27.50 16 47
## 248 27.50 12 50
## 249 11.25 15 22
## 250 11.25 12 50
## 251 37.50 19 60
## 252 18.75 12 40
## 253 32.50 13 50
## 254 23.75 12 47
## 255 32.50 18 50
## 256 32.50 14 34
## 257 32.50 15 37
## 258 16.25 8 35
## 259 18.75 12 16
## 260 32.50 12 49
## 261 21.25 12 45
## 262 80.00 13 50
## 263 45.00 13 37
## 264 13.75 12 16
## 265 9.00 8 41
## 266 23.75 14 29
## 267 23.75 18 55
## 268 11.25 11 48
## 269 11.25 12 33
## 270 11.25 12 34
## 271 5.50 16 56
## 272 5.50 10 32
## 273 16.25 13 36
## 274 2.00 14 37
## 275 13.75 12 12
## 276 80.00 12 41
## 277 9.00 9 41
## 278 9.00 16 47
## 279 11.25 14 39
## 280 45.00 14 33
## 281 32.50 12 40
## 282 13.75 12 37
## 283 11.25 17 76
## 284 32.50 12 34
## 285 45.00 16 39
## 286 27.50 10 28
## 287 5.50 12 19
## 288 23.75 12 32
## 289 18.75 12 34
## 290 37.50 14 50
## 291 27.50 12 50
## 292 16.25 5 28
## 293 13.75 13 37
## 294 45.00 11 72
## 295 37.50 17 69
## 296 18.75 20 55
## 297 37.50 16 34
## 298 32.50 14 48
## 299 32.50 14 41
## 300 27.50 12 41
## 301 13.75 12 29
## 302 11.25 10 47
## 303 9.00 12 47
## 304 13.75 12 47
## 305 32.50 12 56
## 306 32.50 14 50
## 307 27.50 12 47
## 308 18.75 12 47
## 309 0.50 12 37
## 310 21.25 12 37
## 311 27.50 11 54
## 312 23.75 18 60
## 313 13.75 20 69
## 314 13.75 4 34
## 315 23.75 12 49
## 316 23.75 14 50
## 317 16.25 13 41
## 318 9.00 9 41
## 319 23.75 16 69
## 320 16.25 11 45
## 321 11.25 10 32
## 322 80.00 12 41
## 323 32.50 11 29
## 324 13.75 9 40
## 325 11.25 12 50
## 326 11.25 12 49
## 327 32.50 17 62
## 328 21.25 14 45
## 329 23.75 14 48
## 330 23.75 15 69
## 331 3.50 12 37
## 332 18.75 11 38
## 333 11.25 10 40
## 334 21.25 16 63
## 335 45.00 15 40
## 336 32.50 18 43
## 337 55.00 12 37
## 338 45.00 12 51
## 339 11.25 14 26
## 340 45.00 8 50
## 341 80.00 16 76
## 342 37.50 19 41
## 343 16.25 15 43
## 344 16.25 18 36
## 345 45.00 16 50
## 346 27.50 12 33
## 347 37.50 15 32
## 348 18.75 12 33
## 349 23.75 18 43
## 350 37.50 12 39
raw <- read.delim("IncomeRegress_raw.txt", header=FALSE)
# Adding variable names to our data
colnames(raw) <- c("prestig80", "educ", "income")
#Let's specify the model
IncReg <- 'income ~ prestig80 + educ'
#Fitting the model
fit <- sem(model = IncReg, data = raw, meanstructure = TRUE)
#Print to summary of estimated model
summary (fit, fit.measures = TRUE, standardized = TRUE)
## lavaan 0.6.16 ended normally after 1 iteration
##
## Estimator ML
## Optimization method NLMINB
## Number of model parameters 4
##
## Number of observations 350
##
## Model Test User Model:
##
## Test statistic 0.000
## Degrees of freedom 0
##
## Model Test Baseline Model:
##
## Test statistic 126.160
## Degrees of freedom 2
## P-value 0.000
##
## User Model versus Baseline Model:
##
## Comparative Fit Index (CFI) 1.000
## Tucker-Lewis Index (TLI) 1.000
##
## Loglikelihood and Information Criteria:
##
## Loglikelihood user model (H0) -1367.790
## Loglikelihood unrestricted model (H1) -1367.790
##
## Akaike (AIC) 2743.581
## Bayesian (BIC) 2759.012
## Sample-size adjusted Bayesian (SABIC) 2746.323
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.000
## 90 Percent confidence interval - lower 0.000
## 90 Percent confidence interval - upper 0.000
## P-value H_0: RMSEA <= 0.050 NA
## P-value H_0: RMSEA >= 0.080 NA
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.000
##
## Parameter Estimates:
##
## Standard errors Standard
## Information Expected
## Information saturated (h1) model Structured
##
## Regressions:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## income ~
## prestig80 0.152 0.036 4.247 0.000 0.152 0.199
## educ 2.211 0.228 9.690 0.000 2.211 0.455
##
## Intercepts:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .income 8.762 3.054 2.869 0.004 8.762 0.607
##
## Variances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .income 145.193 10.976 13.229 0.000 145.193 0.697
#Printing R-Square separately as it gives an error term in the summary line
lavInspect(fit, "rsquare")
## income
## 0.303
prestig80 = 0.152: for every one unit change in prestige score, there is a 0.152 increase in income (1000’s of dollars) holding education constant, p < .05.
educ = 2.211: for every year increase in education, there is a 2.211 increase in income (1000’s of dollars), holding prestige score constant, p < .05.
R-Square = 0.303: The total variance in income accounted for by its association with prestige score and education. Also called the squared multiple correlation as we have multiple predictors.
# Reading in Covariance Matrix and Means
# Note: I did question 3 before question 2.
mean <- read.csv("IncomeRegress_means.txt")
lower <- readLines("/Users/Muhammad/Desktop/SOCI717/Assignments/Assignment2/IncomeRegress_cov.txt")
Income.Cov <- getCov(lower, names = c( "prestige", "education", "income"))
#Model
Income.model <- '
# latent variables
income =~ prestige + education
# regressions
income ~ prestige + education
# correlated residuals
prestige ~~ education
'
fit2 <- sem(Income.model,
sample.cov = Income.Cov, sample.mean = mean,
sample.nobs = 350)
## Warning in lav_model_vcov(lavmodel = lavmodel, lavsamplestats = lavsamplestats, : lavaan WARNING:
## Could not compute standard errors! The information matrix could
## not be inverted. This may be a symptom that the model is not
## identified.
summary(fit2, standardized = TRUE)
## lavaan 0.6.16 ended normally after 18 iterations
##
## Estimator ML
## Optimization method NLMINB
## Number of model parameters 9
##
## Number of observations 350
##
## Model Test User Model:
##
## Test statistic NA
## Degrees of freedom -4
## P-value (Unknown) NA
##
## Parameter Estimates:
##
## Standard errors Standard
## Information Expected
## Information saturated (h1) model Structured
##
## Latent Variables:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## income =~
## prestige 1.000 5.120 0.271
## education 0.184 NA 0.942 0.317
##
## Regressions:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## income ~
## prestige 0.273 NA 0.053 1.005
## education -0.040 NA -0.008 -0.023
##
## Covariances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .prestige ~~
## .education 0.006 NA 0.006 0.000
##
## Intercepts:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .prestige 11.746 NA 11.746 0.622
## .education 43.424 NA 43.424 14.628
## .income 0.000 0.000 0.000
##
## Variances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .prestige 189.371 NA 189.371 0.532
## .education 8.087 NA 8.087 0.918
## .income 0.058 NA 0.002 0.002
#Printing R-Square separately as it gives an error term in the summary line
lavInspect(fit2, "rsquare")
## prestige education income
## 0.468 0.082 0.998
Prestige = 0.273: for every one unit change in prestige score, there is a 0.273 increase in income (1000’s of dollars) holding education constant, no standard error or p-value provided.
educ = -0.040: for every year increase in education, there is a -.040 decrease in income (1000’s of dollars), holding prestige score constant, no standard error or p-value provided.
Prestige R-Square = .468: The total variance in income accounted for by its association with prestige score is .468.
Education R-Square = .082: The total variance in income accounted for by its association with education score is .082. This is quite low.
Income R-Square = .998, this makes sense as the dependent variable is income and so total variance in income accounted for by its association with itself.
B_prestigeIncome = 1 (set to 1) B_educationIncome = .184
#Reading in Covariance Matrix
lower <- readLines("/Users/Muhammad/Desktop/SOCI717/Assignments/Assignment2/IncomeRegress_cov.txt")
Income.Cov <- getCov(lower, names = c( "prestige", "education","income" ))
#Model
Income.model <- '
# latent variables
income =~ prestige + education
# regressions
income ~ prestige + education
# correlated residuals
prestige ~~ education
'
fit3 <- sem(Income.model,
sample.cov = Income.Cov,
sample.nobs = 350)
## Warning in lav_model_vcov(lavmodel = lavmodel, lavsamplestats = lavsamplestats, : lavaan WARNING:
## Could not compute standard errors! The information matrix could
## not be inverted. This may be a symptom that the model is not
## identified.
summary(fit3, standardized = TRUE)
## lavaan 0.6.16 ended normally after 13 iterations
##
## Estimator ML
## Optimization method NLMINB
## Number of model parameters 7
##
## Number of observations 350
##
## Model Test User Model:
##
## Test statistic NA
## Degrees of freedom -4
## P-value (Unknown) NA
##
## Parameter Estimates:
##
## Standard errors Standard
## Information Expected
## Information saturated (h1) model Structured
##
## Latent Variables:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## income =~
## prestige 1.000 4.719 0.250
## education 0.165 NA 0.778 0.262
##
## Regressions:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## income ~
## prestige 0.232 NA 0.049 0.927
## education 0.286 NA 0.061 0.180
##
## Covariances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .prestige ~~
## .education -0.007 NA -0.007 -0.000
##
## Variances:
## Estimate Std.Err z-value P(>|z|) Std.lv Std.all
## .prestige 203.198 NA 203.198 0.571
## .education 7.258 NA 7.258 0.824
## .income 0.055 NA 0.002 0.002
#Printing R-Square separately as it gives an error term in the summary line
lavInspect(fit3, "rsquare")
## prestige education income
## 0.429 0.176 0.998
Prestige = 0.232: for every one unit change in prestige score, there is a 0.232 increase in income (1000’s of dollars) holding education constant, no standard error or p-value provided.
educ = 0.286: for every year increase in education, there is a 0.286 increase in income (1000’s of dollars), holding prestige score constant, no standard error or p-value provided.
Prestige R-Square = .429: The total variance in income accounted for by its association with prestige score is .429. This is lower than inputting both the covariance matrix and mean vector.
Education R-Square = .176: The total variance in income accounted for by its association with education score is .176. Without the mean vector included, we see a higher R-Square for education.
Income R-Square = .998, this makes sense as the dependent variable is income and so total variance in income accounted for by its association with itself.
B_prestigeIncome = 1 (set to 1) B_educationIncome = .165
Coefficient estimates, Significance, R-square
See interpretations under each respective question.