我在R的'lavaan'软件包中遇到了sem()函数的问题,我希望有人可能会对我如何修复它有所了解。
我的模型设置如下:
mod <- 'trait ~ historical1 + historical 2 + contemporary1 + contemporary2
abundance ~ contemporary1 + contemporary2 + trait
bacteria ~ historical1 + historical 2 + contemporary1 + contemporary2 + trait + abundance'
我的外生变量(历史1,历史2,当代1和当代2)是我为两个可能的水平进行实验操作的分类变量。我将它们视为0和1的虚拟变量。其他一切都是数字测量。
我有三个不同的“细菌”变量,我试图分别看。当我使用第一个(细菌1)时,模型运行平稳。
> modA<-'trait ~ historical1 + historical 2 + contemporary1 + contemporary2
+ abundance ~ contemporary1 + contemporary2 + trait
+ bacteria1 ~ historical1 + historical 2 + contemporary1 + contemporary2 + trait + abundance'
> fit<-sem(modA,data=data,meanstructure=T)
Warning message:
In lav_data_full(data = data, group = group, group.label = group.label, :
lavaan WARNING: some observed variances are (at least) a factor 1000 times larger than others; use varTable(fit) to investigate
> fitmeasures(fit)
fmin chisq df pvalue
0.154 24.028 2.000 0.000
baseline.chisq baseline.df baseline.pvalue cfi
114.474 15.000 0.000 0.779
tli nnfi rfi nfi
-0.661 -0.661 1.000 0.790
pnfi ifi rni logl
0.105 0.804 0.779 -1605.897
unrestricted.logl npar aic bic
-1593.883 19.000 3249.794 3294.572
ntotal bic2 rmsea rmsea.ci.lower
78.000 3234.670 0.376 0.251
rmsea.ci.upper rmsea.pvalue rmr rmr_nomean
0.517 0.000 40.988 45.826
srmr srmr_bentler srmr_bentler_nomean srmr_bollen
0.080 0.080 0.089 0.080
srmr_bollen_nomean srmr_mplus srmr_mplus_nomean cn_05
0.089 0.080 0.089 20.450
cn_01 gfi agfi pgfi
30.899 0.997 0.946 0.057
mfi ecvi
0.868 NA
> summary(fit)
lavaan (0.5-16) converged normally after 145 iterations
Number of observations 78
Estimator ML
Minimum Function Test Statistic 24.028
Degrees of freedom 2
P-value (Chi-square) 0.000
Parameter estimates:
Information Expected
Standard Errors Standard
Estimate Std.err Z-value P(>|z|)
Regressions:
trait ~
historical1 8.722 39.607 0.220 0.826
historical2 89.529 39.607 2.260 0.024
contemporary1 6.737 39.621 0.170 0.865
contemporary2 411.556 39.621 10.387 0.000
abundance ~
contemporary1 -36.390 81.354 -0.447 0.655
contemporary2 424.993 123.301 3.447 0.001
trait -0.251 0.225 -1.116 0.265
bacteria1 ~
historical1 -2.342 2.455 -0.954 0.340
historical2 -2.142 2.533 -0.845 0.398
contemporary1 -0.268 2.459 -0.109 0.913
contemporary2 3.410 4.059 0.840 0.401
trait 0.000 0.007 0.031 0.975
abundance -0.008 0.003 -2.451 0.014
Intercepts:
trait 1299.789 44.285 29.351 0.000
abundance 1438.077 311.643 4.614 0.000
bacteria1 26.805 10.717 2.501 0.012
Variances:
trait 30569.613 4895.056
abundance 128842.663 20631.338
bacteria1 117.383 18.796
然而,当我使用其他两个细菌变量中的任何一个时,我收到警告“无法计算标准错误!”。
> modB<-'trait ~ historical1 + historical 2 + contemporary1 + contemporary2
+ abundance ~ contemporary1 + contemporary2 + trait
+ bacteria2 ~ historical1 + historical 2 + contemporary1 + contemporary2 + trait + abundance'
> fit<-sem(modB,data=data,meanstructure=T)
Warning messages:
1: In lav_data_full(data = data, group = group, group.label = group.label, :
lavaan WARNING: some observed variances are (at least) a factor 1000 times larger than others; use varTable(fit) to investigate
2: In lav_model_vcov(lavmodel = lavmodel, lavsamplestats = lavsamplestats, :
lavaan WARNING: could not compute standard errors!
> fitmeasures(fit)
fmin chisq df pvalue baseline.chisq
0.154 24.028 2.000 0.000 126.602
baseline.df baseline.pvalue cfi tli nnfi
15.000 0.000 0.803 -0.480 -0.480
rfi nfi pnfi ifi rni
1.000 0.810 0.108 0.823 0.803
logl unrestricted.logl npar aic bic
-1277.674 -1265.660 19.000 2593.348 2638.126
ntotal bic2 rmsea rmsea.ci.lower rmsea.ci.upper
78.000 2578.224 0.376 0.251 0.517
rmsea.pvalue rmr rmr_nomean srmr srmr_bentler
0.000 15.075 16.855 0.078 0.078
srmr_bentler_nomean srmr_bollen srmr_bollen_nomean srmr_mplus srmr_mplus_nomean
0.087 0.078 0.087 0.078 0.087
cn_05 cn_01 gfi agfi pgfi
20.450 30.899 0.997 0.945 0.057
mfi ecvi
0.868 NA
> summary(fit)
lavaan (0.5-16) converged normally after 115 iterations
Number of observations 78
Estimator ML
Minimum Function Test Statistic 24.028
Degrees of freedom 2
P-value (Chi-square) 0.000
Parameter estimates:
Information Expected
Standard Errors Standard
Estimate Std.err Z-value P(>|z|)
Regressions:
trait ~
historical1 8.723
historical2 89.530
contemporary1 6.737
contemporary2 411.556
abundance ~
contemporary1 -36.390
contemporary2 424.993
trait -0.251
bacteria2 ~
historical1 0.020
historical2 -0.038
contemporary1 -0.075
contemporary2 -0.071
trait -0.000
abundance -0.000
Intercepts:
trait 1299.788
abundance 1438.077
bacteria2 0.367
Variances:
trait 30569.606
abundance 128842.668
bacteria2 0.026
> modC<-'trait ~ historical1 + historical 2 + contemporary1 + contemporary2
+ abundance ~ contemporary1 + contemporary2 + trait
+ bacteria3 ~ historical1 + historical 2 + contemporary1 + contemporary2 + trait + abundance'
> fit<-sem(modC,data=data,meanstructure=T)
Warning messages:
1: In lav_data_full(data = data, group = group, group.label = group.label, :
lavaan WARNING: some observed variances are (at least) a factor 1000 times larger than others; use varTable(fit) to investigate
2: In lav_model_vcov(lavmodel = lavmodel, lavsamplestats = lavsamplestats, :
lavaan WARNING: could not compute standard errors!
> fitmeasures(fit)
fmin chisq df pvalue baseline.chisq
0.154 24.028 2.000 0.000 112.875
baseline.df baseline.pvalue cfi tli nnfi
15.000 0.000 0.775 -0.688 -0.688
rfi nfi pnfi ifi rni
1.000 0.787 0.105 0.801 0.775
logl unrestricted.logl npar aic bic
-1414.328 -1402.314 19.000 2866.656 2911.434
ntotal bic2 rmsea rmsea.ci.lower rmsea.ci.upper
78.000 2851.532 0.376 0.251 0.517
rmsea.pvalue rmr rmr_nomean srmr srmr_bentler
0.000 15.160 16.949 0.077 0.077
srmr_bentler_nomean srmr_bollen srmr_bollen_nomean srmr_mplus srmr_mplus_nomean
0.086 0.079 0.086 0.079 0.086
cn_05 cn_01 gfi agfi pgfi
20.450 30.899 1.000 0.991 0.057
mfi ecvi
0.868 NA
> summary(fit)
lavaan (0.5-16) converged normally after 119 iterations
Number of observations 78
Estimator ML
Minimum Function Test Statistic 24.028
Degrees of freedom 2
P-value (Chi-square) 0.000
Parameter estimates:
Information Expected
Standard Errors Standard
Estimate Std.err Z-value P(>|z|)
Regressions:
trait ~
historical1 8.723
historical2 89.530
contemporary1 6.737
contemporary2 411.556
abundance ~
contemporary1 -36.390
contemporary2 424.993
trait -0.251
bacteria3 ~
historical1 0.098
historical2 0.095
contemporary1 -0.102
contemporary2 0.204
trait -0.000
abundance -0.000
Intercepts:
trait 1299.788
abundance 1438.077
bacteria3 19.984
Variances:
trait 30569.585
abundance 128842.707
bacteria3 0.864
我对这个警告进行了一些研究,但似乎没有任何帮助。模型是否恰好与其他两个变量非常吻合?
有什么想法吗?提前谢谢。
> str(data)
'data.frame': 78 obs. of 10 variables:
$ X : int 1 2 3 4 5 6 7 8 9 10 ...
$ historical1 : int 0 0 1 1 0 0 1 1 1 1 ...
$ historical2 : int 0 1 1 0 0 1 1 0 0 1 ...
$ contemporary1: int 0 1 0 1 0 0 1 0 1 0 ...
$ contemporary2: int 1 1 0 1 0 0 1 0 1 0 ...
$ trait : num 2202 1935 1371 1978 1130 ...
$ abundance : num 1011 1757 1347 1578 1674 ...
$ bacteria1 : num 30.39 6.69 7.58 6.05 8.47 ...
$ bacteria2 : num -0.0356 0.0304 0.1344 -0.0497 0.265 ...
$ bacteria3 : num 19.4 18.1 20 18.1 18.4 ...
埃里卡
答案 0 :(得分:1)
如果有人想知道,我最终确定了这个。模型中的变量之间显然存在缩放问题。它可以使用scale()函数修复。
data[,c(19,21)]<-scale(data[,c(19,21)],center=FALSE,scale=c(100000000,0.01))
如果第19列对应于细菌2&#39;变量和第21列对应于我的细菌3&#39;变量。细菌2变量大约为10 ^ 7,而细菌3大约为10 ^ -2。将它们重新调整为10 ^ 1或10 ^ 2会修复上面代码中生成的警告,sem()会成功计算出标准错误。