Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 161 lines (121 sloc) 3.932 kb
785ab715 »
2011-04-29 first commit
1 #####
2 # Models from Grace and Bollen 2006 USGS report on SEMS
3 # in Ecology. Provides multiple examples of latent
4 # and composite variable uses. This file is intended
5 # for folk to get a look at how one can implement
6 # composite variables in the lavaan 0.4 tree
7 #
8 # note: only the competition-colonization models included here
9 #
10 # last tested: 4/29/11
11 # last lavaan version tested: 0.4-8
12 # source of models: http://pubs.usgs.gov/of/2006/1363/
13 #
14 # byrnes@nceas.ucsb.edu
15 ####
16
17 library(lavaan)
18 library(MBESS) #MBESS used to convert the reported correlation matrix to cov
19
20
21 #####
22 # use the published correlation matrix and sd
23 # to create a covariance matrix
24
25 #sample size
26 n<-180
27
28 #correlation matrix taken from text
29 bg.cor<-matrix(c(1,NA,NA,NA,NA,NA,NA,
30 -0.5785,1,NA,NA,NA,NA,NA,
31 0.6424,-0.5934,1,NA,NA,NA,NA,
32 -0.2553,0.1844,-0.3146,1,NA,NA,NA,
33 -0.3369,0.4604,-0.3462,0.5767,1,NA,NA,
34 -0.0073,-0.0465,-0.0976,0.1324,0.0265,1,NA,
35 -0.3423,0.207,-0.4062,0.3189,0.2952,0.2394,1),ncol=7)
36
37 rownames(bg.cor)<-colnames(bg.cor)<-c("Col.Freq", "Distance", "Age", "Texture", "Moisture", "pH", "Cover")
38
39 #fill in the correlation matrix
40 bg.cor[which(is.na(bg.cor), arr.ind=T)]<-0
41 diag(bg.cor)<-0.5
42 bg.cor<-bg.cor+t(bg.cor)
43
44 #the standard deviation vector
45 bg.sd<-c(0.1867,0.8207,0.3390,0.4588,1.1359,0.7507,0.3017)
46
47 #convert the correlation matrix and SDs to a covariance matrix
48 bg.cov<-cor2cov(bg.cor, bg.sd)
49
50
51
52 ###
53 # Response stays constant across all models, so
54 # create a latent response submodel that incorporates
55 # the specified measurement error from the report
56 ###
57 #loadings for col freq and cover error
58 #0.1*bg.sd[1]^2 #0.003485689
59 #0.1*bg.sd[7]^2 #0.009102289
60
61 latent.response<-
62 ' #measurement error latents
63 Competitors =~ Cover
64 Colonization =~ Col.Freq
65 Col.Freq ~~ 0.003485689*Col.Freq
66 Cover ~~ 0.009102289*Cover'
67
68 ####
69 # Model A - latent variables
70 ####
71 model.a<-paste(sep="\n", latent.response, '
72 #latent exogenous vars
73 Soil =~ pH + Moisture + Texture
74 Landscape =~ Distance + Age
75 Soil ~~ Landscape
76
77 #regressions
78 Competitors ~ Soil + Landscape
79 Colonization ~ Soil + Landscape + Competitors')
80
81 fit.a<-sem(model.a, sample.cov=bg.cov, sample.nobs=n, std.lv=T)
82 summary(fit.a)
83
84 ####
85 # Model B - Composite variables
86 ####
87 model.b<-paste(sep="\n", latent.response, '
88 #composite exogenous vars
89 Soil ~ 1*pH + Moisture + Texture
90 Soil ~~ 0*Soil
91
92 Landscape ~ 1*Distance + Age
93 Landscape ~~ 0*Landscape
94
95 #LV definitions for regressions
96 Soil =~ I(NA)*Competitors+I(NA)*Colonization
97 Landscape =~ I(NA)*Competitors+I(NA)*Colonization
98
99 #last bit of regression
100 Colonization ~ Competitors
101
102 ')
103
63118a3b »
2011-05-02 Adding Cardinale et al 2009 Ecology example.
104 #note the orthogonal argument to make sure the composites are not correlated
105 fit.b<-sem(model.b, sample.cov=bg.cov, sample.nobs=n, orthogonal=T)
785ab715 »
2011-04-29 first commit
106 fit.b
107
108 summary(fit.b)
109 standardizedSolution(fit.b)
110
111
112 ####
113 # Model C - Observed variables
114 ####
115 model.c<-paste(sep="\n", latent.response, '
116
117 #regression relationships!
118 Competitors ~ pH + Moisture + Texture + Distance + Age
119 Colonization ~ pH + Moisture + Texture + Distance + Age+Competitors')
120
121
122 fit.c<-sem(model.c, sample.cov=bg.cov, sample.nobs=n)
123 summary(fit.c)
124 standardizedSolution(fit.c)
125
126
127 ####
128 # Model D - Multiple Composites
63118a3b »
2011-05-02 Adding Cardinale et al 2009 Ecology example.
129 # still not working...although, says I have 2DF, but paper says model D has 0....
785ab715 »
2011-04-29 first commit
130 ####
131 model.d<-paste(sep="\n", latent.response, '
132 #composite exogenous vars
133 Soil ~ 1*pH + Moisture + Texture
134 Soil ~~ 0*Soil
135
136 Soil2 ~ pH + 1*Moisture + Texture
137 Soil2 ~~ 0*Soil2
138
139 Landscape ~ 1*Distance + Age
140 Landscape ~~ 0*Landscape
141
142 Landscape2 ~ Distance + 1*Age
143 Landscape2 ~~ 0*Landscape2
144
145 #LV definitions for regressions
146 Soil =~ I(NA)*Competitors
147 Landscape =~ I(NA)*Competitors
148
149 Soil2 =~ I(NA)*Colonization
150 Landscape2 =~ I(NA)*Colonization
151
152
153 #last bit of regression
154 Colonization ~ Competitors
63118a3b »
2011-05-02 Adding Cardinale et al 2009 Ecology example.
155
785ab715 »
2011-04-29 first commit
156 ')
157
63118a3b »
2011-05-02 Adding Cardinale et al 2009 Ecology example.
158 fit.d<-sem(model.d, sample.cov=bg.cov, sample.nobs=n, orthogonal=TRUE)
159
785ab715 »
2011-04-29 first commit
160 summary(fit.d)
161 standardizedSolution(fit.d)
Something went wrong with that request. Please try again.