[英]lsmeans for a piecewise linear mixed-effects model on r
我最初在 Cross Validated Stackexchange 上發布了這個問題,但沒有得到答案。 因此我決定在這里給它一個 go。 我試圖弄清楚如何獲得具有隨機截距和斜率的分段線性混合效應 model(配備 nlme 包)的 lsmeans。 我的數據代表了一組男性和女性學生在引入日常冥想之前和之后每周參加考試的數學成績。 創建數據框並適合 model 的最小可重現示例如下:
library(nlme)
library("lsmeans")
# Subject's ID
ID <- c(1,1,1,
2,2,2,
3,3,3,
4,4,4,
5,5,5,
6,6,6,
7,7,7,
8,8,8)
# Time (weeks) before introduction of routine
time1 <- c(-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0)
# Time (weeks) before introduction of routine
time2 <- c(0,0,1,
0,0,1,
0,0,1,
0,0,1,
0,0,1,
0,0,1,
0,0,1,
0,0,1)
# week test math scores
mscore <- c(80,92,73,
75,80,85,
60,75,70,
75,80,75,
78,84,75,
78,91,95,
64,72,71,
84,92,70)
# create dataframe
longdata <-data.frame(ID, time1, time2, mscore)
head(longdata)
# fit model
pwmodel <- lme(mscore ~ time1+time2,
random =~ time1+time2|ID,
data=longdata,
method="ML")
# calculate marginal means:
#with both variables
lsmeans(pwmodel, ~(time1+time2),
at=list(time1=c(-1,0,1), time2=c(-1,0,1)) )
# only with one variable
lsmeans(pwmodel, ~time1,
at=list(time1=c(-1,0,1) ))
這里的 time1 和 time2 代表每日冥想程序開始之前和之后的時間。
問題是:在 -1、0 和 1 時從這個 model 獲取 lsmeans(或 emmeans,如果更好的話)的正確方法是什么? 考慮兩個時間變量還是僅考慮其中一個(time1 或 time2)?
兩種方法的輸出如下所示:
> #with both variables
> lsmeans(pwmodel, ~(time1+time2),
+ at=list(time1=c(-1,0,1), time2=c(-1,0,1)) )
time1 time2 lsmean SE df lower.CL upper.CL
-1 -1 80.8 5.46 7 67.8 93.7
0 -1 89.8 5.47 7 76.8 102.7
1 -1 98.8 5.81 7 85.0 112.5
-1 0 74.2 2.88 7 67.4 81.1
0 0 83.2 2.77 7 76.7 89.8
1 0 92.2 3.28 7 84.5 100.0
-1 1 67.8 3.34 7 59.9 75.6
0 1 76.8 3.12 7 69.4 84.1
1 1 85.8 3.47 7 77.5 94.0
Degrees-of-freedom method: containment
Confidence level used: 0.95
> # only with one variable
> lsmeans(pwmodel, ~time1,
+ at=list(time1=c(-1,0,1) ))
time1 lsmean SE df lower.CL upper.CL
-1 71 2.59 7 64.9 77.1
0 80 2.38 7 74.4 85.6
1 89 2.89 7 82.2 95.8
Results are averaged over the levels of: time2
Degrees-of-freedom method: containment
Confidence level used: 0.95
它們顯然返回不同的結果,但兩種方式不應該給出相同的值嗎?
你有一個非常尷尬的參數化,因為實際上只有一個“時間”概念,而不是兩個。 我建議定義一個只有time
變量的數據集,並擬合一個 model 來解釋 0 處的斷點:
library(nlme)
library(emmeans)
dat <- data.frame(
ID = rep(1:8, each = 3),
time = rep(-1:1, 8),
mscore = c(80,92,73,
75,80,85,
60,75,70,
75,80,75,
78,84,75,
78,91,95,
64,72,71,
84,92,70)
)
mod <- lme(mscore ~ time:(1 + (time > 0)), ~ time|ID, data = dat)
lsmeans(mod, "time", cov.reduce = FALSE)
## time lsmean SE df lower.CL upper.CL
## -1 74.2 3.20 7 66.7 81.8
## 0 83.2 2.68 7 76.9 89.6
## 1 76.8 2.88 7 70.0 83.5
##
## Degrees-of-freedom method: containment
## Confidence level used: 0.95
emmip(mod, ~ time, at = list(time = seq(-1, 1, by = .25)))
由代表 package (v2.0.0) 於 2021 年 11 月 20 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.