简体   繁体   中英

How to extract the actual values of parameters and their standard error instead of the marginal effect estimates in lmer from package lme4 in R?

I have this fake dataset that describes the effect of air temperature on the growth of two plant species (a and b).

data1 <- read.csv(text = "
year,block,specie,temperature,growth
2019,1,a,0,7.217496163
2019,1,a,1,2.809792001
2019,1,a,2,16.09505635
2019,1,a,3,24.52673264
2019,1,a,4,49.98455022
2019,1,a,5,35.78568291
2019,2,a,0,8.332533323
2019,2,a,1,16.5997836
2019,2,a,2,11.95833966
2019,2,a,3,34.4
2019,2,a,4,54.19081002
2019,2,a,5,41.1291734
2019,1,b,0,14.07939683
2019,1,b,1,13.73257973
2019,1,b,2,31.33076651
2019,1,b,3,44.81995622
2019,1,b,4,79.27999184
2019,1,b,5,75.0527336
2019,2,b,0,14.18896232
2019,2,b,1,29.00692747
2019,2,b,2,27.83736734
2019,2,b,3,61.46006916
2019,2,b,4,93.91100024
2019,2,b,5,92.47922985
2020,1,a,0,4.117536842
2020,1,a,1,12.70711508
2020,1,a,2,16.09570046
2020,1,a,3,29.49417491
2020,1,a,4,35.94571498
2020,1,a,5,50.74477018
2020,2,a,0,3.490585144
2020,2,a,1,3.817105315
2020,2,a,2,22.43112718
2020,2,a,3,14.4
2020,2,a,4,46.84223604
2020,2,a,5,39.10398717
2020,1,b,0,10.17712428
2020,1,b,1,22.04514586
2020,1,b,2,30.37221799
2020,1,b,3,51.80333619
2020,1,b,4,76.22765452
2020,1,b,5,78.37284714
2020,2,b,0,7.308139613
2020,2,b,1,22.03241605
2020,2,b,2,45.88385871
2020,2,b,3,30.43669633
2020,2,b,4,76.12904988
2020,2,b,5,85.9324324
")

The experiment was conducted two years and in a block design (nested within years). The goal is to inform how much growth is affected per unit of change in temperature. Also, the is a need to provide a measure of uncertainty (standard error) for this estimate. The same needs to be done for the growth recorded at zero degrees of temperature.

library(lme4)
library(lmerTest)
library(lsmeans)

test.model.1 <- lmer(growth ~ 
                       specie +
                       temperature +
                       specie*temperature +
                       (1|year) +
                       (1|year:block),
                     data= data1,
                     REML=T,
                     control=lmerControl(check.nobs.vs.nlev = "ignore",
                                         check.nobs.vs.rankZ = "ignore",
                                         check.nobs.vs.nRE="ignore")) 
summary(test.model.1)

The summary give me this output for the fixed effect:

Linear mixed model fit by REML. t-tests use Satterthwaite's method ['lmerModLmerTest']
Formula: growth ~ specie + temperature + specie * temperature + (1 | year) +  
    (1 | year:block)
   Data: data1
Control: lmerControl(check.nobs.vs.nlev = "ignore", check.nobs.vs.rankZ = "ignore",  
    check.nobs.vs.nRE = "ignore")

REML criterion at convergence: 331.3

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.6408 -0.7637  0.1516  0.5248  2.4809 

Random effects:
 Groups     Name        Variance Std.Dev.
 year:block (Intercept)  6.231   2.496   
 year       (Intercept)  0.000   0.000   
 Residual               74.117   8.609   
Number of obs: 48, groups:  year:block, 4; year, 2

Fixed effects:
                    Estimate Std. Error     df t value Pr(>|t|)    
(Intercept)            2.699      3.356 26.256   0.804    0.428    
specieb                4.433      4.406 41.000   1.006    0.320    
temperature            8.624      1.029 41.000   8.381  2.0e-10 ***
specieb:temperature    7.088      1.455 41.000   4.871  1.7e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Correlation of Fixed Effects:
            (Intr) specib tmprtr
specieb     -0.656              
temperature -0.767  0.584       
spcb:tmprtr  0.542 -0.826 -0.707
optimizer (nloptwrap) convergence code: 0 (OK)
boundary (singular) fit: see help('isSingular')

From this I can get the growth at 0 degrees of temperature for specie "a" (2.699) , and for specie "b" (2.699 + 4.443 = 7.132) . Also, the rate of change in growth per unit change in temperature is (8.624) for species "a" and (8.624 + 7.088 = 15.712) . The problem I have is that the standard deviation reported in summary() is for the marginal estimate, not for the actual value of the parameter. For instance, the standard error for 4.443 (specieb) is 4.406.. but that is not the standard error for the actual growth at 0 degrees for specie b that is 7.132. What I am looking for is the standard error of let's say 7.132. Also, I'd be nice to have all the calculations I did by hand automatically performed.

I was trying making some tries with emmeans() from lsmeans package but I didn't succeed.

emmeans(test.model.1, growth ~ specie*temperature) 

Error:

Error in contrast.emmGrid(object = new("emmGrid", model.info = list(call = lmer(formula = growth ~  : 
  Contrast function 'growth.emmc' not found

I think your main problem is that you don't need the response variable on the left side of the formula you give to emmeans (the package assumes that you're going to use the same response variable as in the original model,) The left-hand side of the formula is reserved for specifying contrasts. eg pairwise ~... - see help("contrast-methods", package = "emmeans") .

I think you might be looking for:

emmeans(test.model.1, ~specie, at = list(temperature=0))
NOTE: Results may be misleading due to involvement in interactions
 specie emmean   SE   df lower.CL upper.CL
 a        2.70 3.36 11.3   -4.665     10.1
 b        7.13 3.36 11.3   -0.232     14.5

Degrees-of-freedom method: kenward-roger 
Confidence level used: 0.95

If you don't specify the value of temperature, then emmeans uses (I think) the overall average temperature.

For slopes, you want emtrends :

emtrends(test.model.1, ~specie, var = "temperature")
 specie temperature.trend   SE df lower.CL upper.CL
 a                   8.62 1.03 41     6.55     10.7
 b                  15.71 1.03 41    13.63     17.8

Degrees-of-freedom method: kenward-roger 
Confidence level used: 0.95 

I highly recommend the extensive and clearly written vignettes for the emmeans package. Since emmeans has so many capabilities it may take a little while to find the answers to your precise questions, but the effort will be repaid in the long term.

As a small picky point, I would say that what summary() gives you are the "actual" parameters that R uses internally, and what emmeans() gives you are the marginal means (as suggested by the name of the package — e xpected m arginal means...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM