R Markdown

library(lgrdata)
data("hfeifbytree")
hfeifbytree$day = with(hfeifbytree,as.numeric(as.Date(Date)-min(as.Date(Date))))
hfeifbytree$irrigated = ifelse(hfeifbytree$treat %in% c("I","IL"),"Yes","No")
hfeifbytree$fertilized = ifelse(hfeifbytree$treat %in% c("F","IL"),"Yes","No")
names(hfeifbytree)=sub("plotnr","plot",names(hfeifbytree))
hfeifbytree = hfeifbytree[c("height","diameter","irrigated","fertilized","day","plot")]
head(hfeifbytree)
##   height diameter irrigated fertilized day plot
## 1   2.88     2.18        No         No   0   17
## 2   2.52     1.55        No         No   0   25
## 3   2.34     1.52        No         No   0   17
## 4   2.32     1.56        No         No   0    9
## 5   2.28     1.34        No         No   0    7
## 6   2.26     1.45        No         No   0   17

1

par(mfrow=c(1,2))
boxplot(hfeifbytree$height~hfeifbytree$irrigated)
boxplot(hfeifbytree$height~hfeifbytree$fertilized)

2

boxplot(hfeifbytree$height~hfeifbytree$fertilized+hfeifbytree$irrigated)

From the boxplot of the height vs the interation of fertlized and irrigated, we observe that, the heighted is only related to the irrigated rather than the fertilized and the interaction. So I think the interaction term is not significant.

3

plot(hfeifbytree$day,hfeifbytree$height)

From the plot, I observe that, as day increases, there is a positive trend for the height.

4

library(lme4)
## 载入需要的程辑包:Matrix
model <- lmer(height~irrigated+fertilized+day+irrigated:fertilized+(1|plot),data=hfeifbytree)

5

summary(model)
## Linear mixed model fit by REML ['lmerMod']
## Formula: height ~ irrigated + fertilized + day + irrigated:fertilized +  
##     (1 | plot)
##    Data: hfeifbytree
## 
## REML criterion at convergence: 22661.5
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -8.1901 -0.6126  0.0529  0.6429  3.6595 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  plot     (Intercept) 0.3442   0.5867  
##  Residual             1.9155   1.3840  
## Number of obs: 6473, groups:  plot, 16
## 
## Fixed effects:
##                             Estimate Std. Error t value
## (Intercept)                1.133e+00  2.961e-01   3.826
## irrigatedYes               1.357e+00  4.177e-01   3.248
## fertilizedYes              2.588e-04  4.177e-01   0.001
## day                        8.673e-03  3.945e-05 219.863
## irrigatedYes:fertilizedYes 3.570e-01  5.907e-01   0.604
## 
## Correlation of Fixed Effects:
##             (Intr) irrgtY frtlzY day   
## irrigatedYs -0.705                     
## fertilizdYs -0.705  0.500              
## day         -0.072  0.000 -0.001       
## irrgtdYs:fY  0.499 -0.707 -0.707  0.001

6

The total variance expalianed by the random effect is

0.3442/(0.3442+1.9155)
## [1] 0.1523211

7

For the intercept

c(1.133+1.96*0.2961,1.133+1.96*0.2961)
## [1] 1.713356 1.713356

For irrigated(Yes),

c(1.357-1.96*0.4177,1.357+1.96*0.4177)
## [1] 0.538308 2.175692

For fertilized(Yes),

c(2.588e-04-1.96*0.4177,2.588e-04+1.96*0.4177)
## [1] -0.8184332  0.8189508

For Day,

c(8.673e-03-1.96*3.945e-05, 8.673e-03+1.96*3.945e-05)
## [1] 0.008595678 0.008750322

For the interaction

c(0.357-1.96*0.5907,0.357+1.96*0.5907)
## [1] -0.800772  1.514772

8

No. The confidence interval contains 0, thus the interaction terms is not significant.

9

The irrigated and day are significant.

10

Keeping other terms fixed, compared to irrigated(no), the height of irrigated(yes) will increase 1.357 on average.

Keeping other terms fixed, when day increases 1, the height will increase 1.357 on average.

11

plot(model)

This residual plot does not indicate any deviations from a linear form. However, the constant variance assumption may be violated.