lme3<-lmer(Richness~1+NAP+(0+NAP|Beach) ,data =df_long)summary(lme3)#> Linear mixed model fit by REML ['lmerMod']#> Formula: Richness ~ 1 + NAP + (0 + NAP | Beach)#> Data: df_long#> #> REML criterion at convergence: 252.2#> #> Scaled residuals: #> Min 1Q Median 3Q Max #> -1.2182 -0.6636 -0.1930 0.3253 3.3347 #> #> Random effects:#> Groups Name Variance Std.Dev.#> Beach NAP 0.00 0.00 #> Residual 17.31 4.16 #> Number of obs: 45, groups: Beach, 9#> #> Fixed effects:#> Estimate Std. Error t value#> (Intercept) 6.6857 0.6578 10.164#> NAP -2.8669 0.6307 -4.545#> #> Correlation of Fixed Effects:#> (Intr)#> NAP -0.333#> optimizer (nloptwrap) convergence code: 0 (OK)#> boundary (singular) fit: see help('isSingular')nlme3<-lme(Richness~1+NAP,random=~0+NAP|Beach ,data =df_long)summary(nlme3)#> Linear mixed-effects model fit by REML#> Data: df_long #> AIC BIC logLik#> 260.201 267.2458 -126.1005#> #> Random effects:#> Formula: ~0 + NAP | Beach#> NAP Residual#> StdDev: 0.0001127408 4.159929#> #> Fixed effects: Richness ~ 1 + NAP #> Value Std.Error DF t-value p-value#> (Intercept) 6.685662 0.6577579 35 10.164320 0e+00#> NAP -2.866853 0.6307186 35 -4.545376 1e-04#> Correlation: #> (Intr)#> NAP -0.333#> #> Standardized Within-Group Residuals:#> Min Q1 Med Q3 Max #> -1.2181663 -0.6636488 -0.1930031 0.3253447 3.3347473 #> #> Number of Observations: 45#> Number of Groups: 9
19.6 随机效应模型
Code
lme4<-lmer(Richness~1+(1|Beach) ,data =df_long)summary(lme4)#> Linear mixed model fit by REML ['lmerMod']#> Formula: Richness ~ 1 + (1 | Beach)#> Data: df_long#> #> REML criterion at convergence: 261.1#> #> Scaled residuals: #> Min 1Q Median 3Q Max #> -1.7797 -0.5070 -0.0980 0.2547 3.8063 #> #> Random effects:#> Groups Name Variance Std.Dev.#> Beach (Intercept) 10.48 3.237 #> Residual 15.51 3.938 #> Number of obs: 45, groups: Beach, 9#> #> Fixed effects:#> Estimate Std. Error t value#> (Intercept) 5.689 1.228 4.631nlme4<-lme(Richness~1 ,random=~1|Beach ,data =df_long)summary(nlme4)#> Linear mixed-effects model fit by REML#> Data: df_long #> AIC BIC logLik#> 267.1142 272.4668 -130.5571#> #> Random effects:#> Formula: ~1 | Beach#> (Intercept) Residual#> StdDev: 3.237112 3.938415#> #> Fixed effects: Richness ~ 1 #> Value Std.Error DF t-value p-value#> (Intercept) 5.688889 1.228419 36 4.631066 0#> #> Standardized Within-Group Residuals:#> Min Q1 Med Q3 Max #> -1.77968689 -0.50704111 -0.09795286 0.25468670 3.80631705 #> #> Number of Observations: 45#> Number of Groups: 9
# 拟合线性混合模型model<-lme1# 1. 残差图residuals<-resid(model)fitted<-fitted(model)ggplot(data.frame(fitted, residuals), aes(fitted, residuals))+geom_point()+geom_smooth(method ="loess", se =FALSE)+labs(title ="Residuals vs Fitted", x ="Fitted values", y ="Residuals")