[英]Time-series mixed effect model (lme4)
I am trying to run a mixed effects model that uses time as a fixed effect.我正在尝试运行使用时间作为固定效果的混合效果 model。 I have repeated measures taken over irregular time intervals (3-7) and want to account for the fixed linear relationship my variables have with time.
我已经在不规则的时间间隔 (3-7) 内重复采取了措施,并想考虑我的变量与时间的固定线性关系。 At the same time I am interested in determining treatment effects (drought and competition).
同时,我对确定治疗效果(干旱和竞争)感兴趣。
Below is one of my datasets I am trying to run this on using lme4
and lmer
下面是我尝试使用
lme4
和lmer
运行的数据集之一
> BRCAgassydays
Species Drought Competition Treatment Time assimilation conductance intercellularcarbon
1 BRCA Control Invasive CxI 0 7.811799 0.16297273 297.5562
2 BRCA Control Invasive CxI 21 5.405663 0.19472180 314.8806
3 BRCA Control Invasive CxI 29 7.604270 0.14460617 291.1411
4 BRCA Control Invasive CxI 34 7.513887 0.22543327 326.1150
5 BRCA Control Invasive CxI 42 6.683802 0.18940180 318.6928
6 BRCA Control Invasive CxI 55 6.071712 0.13774260 301.6228
7 BRCA Control Invasive CxI 70 6.331053 0.13962460 306.8503
8 BRCA Control Invasive CxI 78 4.941679 0.13157067 312.1904
9 BRCA Control Invasive CxI 82 4.729761 0.11871700 313.6009
10 BRCA Control Invasive CxI 88 6.831296 0.16134250 305.1969
11 BRCA Control Invasive CxI 97 4.652050 0.09225767 295.9346
12 BRCA Control Invasive CxI 104 5.873223 0.16265633 313.8243
13 BRCA Control None CxN 0 7.644604 0.17184619 301.8665
14 BRCA Control None CxN 21 8.250492 0.23745253 317.0501
15 BRCA Control None CxN 29 7.463330 0.13094473 286.2365
16 BRCA Control None CxN 34 7.928604 0.24988353 328.3139
17 BRCA Control None CxN 42 7.415549 0.18531760 309.8957
18 BRCA Control None CxN 55 6.764483 0.13508080 291.4254
19 BRCA Control None CxN 70 5.666392 0.11958453 304.4248
20 BRCA Control None CxN 78 7.267130 0.16822320 303.4725
21 BRCA Control None CxN 82 5.870902 0.11838573 297.4116
22 BRCA Control None CxN 88 7.400286 0.18886560 306.0608
23 BRCA Control None CxN 97 5.397562 0.13183013 313.4487
24 BRCA Control None CxN 104 5.756836 0.14453173 311.5725
25 BRCA Drought Invasive DxI 0 6.932256 0.12224355 285.7301
26 BRCA Drought Invasive DxI 21 8.448956 0.22943413 311.6504
27 BRCA Drought Invasive DxI 29 7.410476 0.12434440 280.5574
28 BRCA Drought Invasive DxI 34 8.208636 0.26668580 327.9786
29 BRCA Drought Invasive DxI 42 5.324922 0.12691907 312.5066
30 BRCA Drought Invasive DxI 55 5.196439 0.10962533 295.7930
31 BRCA Drought Invasive DxI 70 4.643326 0.08082647 289.0008
32 BRCA Drought Invasive DxI 78 3.675965 0.07471427 298.9459
33 BRCA Drought Invasive DxI 82 4.113252 0.09586540 310.3563
34 BRCA Drought Invasive DxI 88 4.340185 0.09807740 310.9444
35 BRCA Drought Invasive DxI 97 4.410509 0.09351200 304.7900
36 BRCA Drought Invasive DxI 104 2.961152 0.06973620 309.7580
37 BRCA Drought None DxN 0 5.299206 0.10402717 299.4199
38 BRCA Drought None DxN 21 8.931698 0.22541307 310.6568
39 BRCA Drought None DxN 29 6.490820 0.10163740 271.2123
40 BRCA Drought None DxN 34 6.748470 0.19204680 323.1626
41 BRCA Drought None DxN 42 4.175082 0.08008673 295.1853
42 BRCA Drought None DxN 55 4.740064 0.10071627 293.5214
43 BRCA Drought None DxN 70 5.147252 0.09366540 284.0992
44 BRCA Drought None DxN 78 5.187626 0.09444033 291.6765
45 BRCA Drought None DxN 82 4.608225 0.09833660 303.1695
46 BRCA Drought None DxN 88 6.398861 0.14120963 303.1060
47 BRCA Drought None DxN 97 3.980145 0.11448827 324.5742
48 BRCA Drought None DxN 104 5.233092 0.12851007 313.0436
transpiration seA seG seCi seE
1 2.822450 0.43644138 0.05761956 105.2020 0.9978866
2 2.320922 0.45469707 0.08708224 140.8189 1.0379479
3 1.790830 0.47508738 0.05903522 118.8578 0.7311035
4 2.400032 0.43793202 0.10081682 145.8431 1.0733268
5 2.805418 0.67185683 0.08470306 142.5237 1.2546211
6 3.114693 0.74197040 0.06160036 134.8898 1.3929330
7 1.795997 1.23465637 0.06244202 137.2276 0.8031942
8 2.715327 0.72150617 0.05884019 139.6158 1.2143311
9 2.544619 0.74568747 0.05309186 140.2466 1.1379881
10 3.019007 1.51528270 0.08067125 152.5985 1.5095034
11 1.784082 0.03402206 0.04612883 147.9673 0.8920409
12 3.453956 0.33488091 0.07274212 140.3465 1.5446562
13 2.598881 0.46217235 0.06075680 106.7259 0.9188432
14 2.909069 0.84976554 0.10619200 141.7891 1.3009752
15 2.025599 0.50776426 0.05856027 128.0088 0.9058755
16 2.487442 0.54267771 0.11175131 146.8264 1.1124180
17 2.875295 0.11954494 0.08287655 138.5896 1.2858709
18 3.265247 0.84720688 0.06040997 130.3294 1.4602628
19 1.483867 0.44247329 0.05347983 136.1429 0.6636057
20 3.203109 0.74145804 0.07523170 135.7170 1.4324737
21 2.539916 1.02080831 0.05294371 133.0065 1.1358850
22 3.553893 0.34056152 0.08446326 136.8745 1.5893495
23 1.926768 0.80018829 0.05895623 140.1785 0.8616768
24 3.047124 0.42481340 0.06463656 139.3395 1.3627154
25 1.899871 0.55859666 0.04321962 101.0208 0.6717057
26 2.797467 0.88094476 0.10260606 139.3743 1.2510653
27 2.110417 0.64763904 0.05560851 125.4691 0.9438074
28 2.689897 0.42303271 0.11926552 146.6765 1.2029587
29 2.197370 0.97036269 0.05675993 139.7572 0.9826938
30 2.669697 0.54886662 0.04902594 132.2827 1.1939247
31 1.295458 0.61865489 0.03614669 129.2451 0.5793462
32 1.736331 0.69632750 0.03341324 133.6927 0.7765110
33 2.171248 0.58167406 0.04287231 138.7956 0.9710117
34 2.127337 1.02370322 0.04386155 139.0585 0.9513742
35 1.719165 0.61438695 0.04181984 136.3062 0.7688341
36 1.628725 1.18038670 0.03118698 138.5280 0.7283880
37 1.572679 0.73317733 0.03677916 105.8609 0.5560261
38 2.887991 0.75700712 0.10080779 138.9300 1.2915488
39 1.712095 0.86022203 0.04545363 121.2898 0.7656720
40 2.074472 0.31120772 0.08588594 144.5227 0.9277320
41 1.466688 0.40419493 0.03581588 132.0109 0.6559227
42 2.380746 0.70855853 0.04504168 131.2668 1.0647018
43 1.520280 0.62697587 0.04188844 127.0530 0.6798899
44 1.772095 0.45202487 0.04223500 130.4417 0.7925049
45 2.191443 0.55772642 0.04397746 135.5815 0.9800431
46 2.756795 1.13005016 0.06315087 135.5531 1.2328762
47 2.051825 0.83631901 0.05120071 145.1540 0.9176041
48 2.867810 1.07582755 0.05747145 139.9974 1.2825237
I am having a hard time figuring out if I have coded it correctly:我很难确定我是否正确编码:
brcalme <- lmer(assimilation ~ Competition*Drought + (1|Time), data = BRCAgassydays)
I am also having a hard time interpreting the results and determining whether they are significant without a p-value我也很难解释结果并在没有 p 值的情况下确定它们是否显着
> brcalme
Linear mixed model fit by REML ['lmerMod']
Formula: assimilation ~ Competition * Drought + (1 | Time)
Data: BRCAgassydays
REML criterion at convergence: 147.2326
Random effects:
Groups Name Std.Dev.
Time (Intercept) 1.0027
Residual 0.9269
Number of obs: 48, groups: Time, 12
Fixed Effects:
(Intercept) CompetitionNone
6.2042 0.6980
DroughtDrought CompetitionNone:DroughtDrought
-0.7320 -0.5918
summary(brcalme)
Linear mixed model fit by REML ['lmerMod']
Formula: assimilation ~ Competition * Drought + (1 | Time)
Data: BRCAgassydays
REML criterion at convergence: 147.2
Scaled residuals:
Min 1Q Median 3Q Max
-2.3906 -0.4165 0.0132 0.5943 2.0888
Random effects:
Groups Name Variance Std.Dev.
Time (Intercept) 1.0055 1.0027
Residual 0.8591 0.9269
Number of obs: 48, groups: Time, 12
Fixed effects:
Estimate Std. Error t value
(Intercept) 6.2042 0.3942 15.739
CompetitionNone 0.6980 0.3784 1.845
DroughtDrought -0.7320 0.3784 -1.935
CompetitionNone:DroughtDrought -0.5918 0.5351 -1.106
Correlation of Fixed Effects:
(Intr) CmpttN DrghtD
CompetitnNn -0.480
DroghtDrght -0.480 0.500
CmpttnNn:DD 0.339 -0.707 -0.707
Thanks in advance for any help!提前感谢您的帮助!
Regarding the coding/instructions for the lmer()
function, you do have valid inputs.关于
lmer()
function 的编码/指令,您确实有有效的输入。 If they are correct or useful depends ultimately on the theory you are using/testing.它们是否正确或有用最终取决于您正在使用/测试的理论。 For such subject matter-specific or theoretical questions, look at CrossValidated.
对于此类特定主题或理论问题,请查看 CrossValidated。
Regarding how to obtain the p-values, load the package lmerTest
and then run summary()
on the model.关于如何获取 p 值,请加载 package
lmerTest
,然后在 model 上运行summary()
。
library(lme4)
library(lmerTest)
lmm <- lmer(assimilation ~ Competition*Drought + (1|Time), data = brcalme)
summary(lmm)
Output: Output:
Linear mixed model fit by REML. t-tests use Satterthwaite's method ['lmerModLmerTest']
Formula: assimilation ~ Competition * Drought + (1 | Time)
Data: brcalme
REML criterion at convergence: 147.2
Scaled residuals:
Min 1Q Median 3Q Max
-2.3906 -0.4165 0.0132 0.5943 2.0888
Random effects:
Groups Name Variance Std.Dev.
Time (Intercept) 1.0055 1.0027
Residual 0.8591 0.9269
Number of obs: 48, groups: Time, 12
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 6.2042 0.3942 23.4989 15.739 5.61e-14 ***
CompetitionNone 0.6980 0.3784 33.0000 1.845 0.0741 .
DroughtDrought -0.7320 0.3784 33.0000 -1.935 0.0617 .
CompetitionNone:DroughtDrought -0.5918 0.5351 33.0000 -1.106 0.2768
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Correlation of Fixed Effects:
(Intr) CmpttN DrghtD
CompetitnNn -0.480
DroghtDrght -0.480 0.500
CmpttnNn:DD 0.339 -0.707 -0.707
Regarding "I am trying to run a mixed effects model that uses time as a fixed effect. I have repeated measures taken over irregular time intervals (3-7) and want to account for the fixed linear relationship my variables have with time."关于“我正在尝试运行使用时间作为固定效果的混合效果 model。我在不规则的时间间隔(3-7)内重复采取了措施,并希望考虑我的变量与时间的固定线性关系。” No you have not coded it correctly.
不,您没有正确编码。
(1|Time)
is a random effects term (see these references:https://cran.r-project.org/web/packages/lme4/vignettes/lmer.pdf , https://stats.stackexchange.com/questions/4700/what-is-the-difference-between-fixed-effect-random-effect-and-mixed-effect-mode ) (1|Time)
is a random effects term (see these references:https: //cran.r-project.org/web/packages/lme4/vignettes/lmer.pdf , https://stats.stackexchange.com/questions /4700/固定效果随机效果和混合效果模式之间的差异是什么)
This random effects term is to group data points in your "repeated-measures".这个随机效应术语是对“重复测量”中的数据点进行分组。 For example if you survey 20 different sites multiple times you might use
(1|Sites)
as a random effects term (see lme4 vignette link above for syntax help).例如,如果您多次调查 20 个不同的站点,您可能会使用
(1|Sites)
作为随机效应术语(有关语法帮助,请参见上面的 lme4 vignette 链接)。 If you really do have repeated-measures it would be good to group your data this way (helps avoid pseudo-replication).如果您确实有重复测量,最好以这种方式对数据进行分组(有助于避免伪复制)。
For Time, you can fit it as a fixed effect:对于时间,您可以将其拟合为固定效果:
brcalme <- lmer(assimilation ~ Competition*Drought + Time, data = BRCAgassydays)
But here you might get an error because you haven't specified any random effects (Time is a fixed effect in the equation above).但是在这里你可能会得到一个错误,因为你没有指定任何随机效应(时间是上面等式中的固定效应)。 So, all you need is a linear model (not mixed-effect).
因此,您只需要一个线性 model(非混合效果)。 You can change
lmer
to lm
to do this:您可以将
lmer
更改为lm
来执行此操作:
brcalme <- lm(assimilation ~ Competition*Drought + Time, data = BRCAgassydays)
But again, fit random effects terms if you can / is appropriate (but based on your question, I am guessing Time is not what you want as a random effects term).但是,如果可以/合适的话,再次拟合随机效应术语(但根据你的问题,我猜时间不是你想要的随机效应术语)。
Regarding p values, it would be worth reading up on what these really mean (eg, P-value, significance level and hypothesis , https://stats.stackexchange.com/questions/166323/misunderstanding-ap-value ), and trying to learn to interpret your estimates and SE around those estimates (the magnitude of effects) rather than looking for 'statistical significance'.关于 p 值,值得阅读这些真正的含义(例如, P 值、显着性水平和假设, https://stats.stackexchange.com/questions/166323/misunderstanding-ap-value ),并尝试学习围绕这些估计(影响的大小)解释您的估计和 SE,而不是寻找“统计意义”。 There is a reason that lme4 doesn't come with p value capabilities out of the box (ie, you have to load
lmerTest
). lme4 没有开箱即用的 p 值功能是有原因的(即,您必须加载
lmerTest
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.