![](/img/trans.png)
[英]How do you fit a linear mixed model with an AR(1) random effects correlation structure in R?
[英]How to fit a random effects model with Subject as random in R?
給出以下形式的數據
myDat = structure(list(Score = c(1.84, 2.24, 3.8, 2.3, 3.8, 4.55, 1.13,
2.49, 3.74, 2.84, 3.3, 4.82, 1.74, 2.89, 3.39, 2.08, 3.99, 4.07,
1.93, 2.39, 3.63, 2.55, 3.09, 4.76), Subject = c(1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L,
7L, 7L, 8L, 8L, 8L), Condition = c(0L, 0L, 0L, 1L, 1L, 1L, 0L,
0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L,
1L), Time = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L)), .Names = c("Score",
"Subject", "Condition", "Time"), class = "data.frame", row.names = c(NA,
-24L))
我想將得分作為主題,條件和時間的函數來建模。 每個(人)受試者的分數測量三次,由變量Time表示,所以我有重復測量。
如何構建R隨機效應模型,主題效果隨機擬合?
ADDENDUM :有人問我是如何生成這些數據的。 你猜對了,數據是假的,因為這一天很長。 得分是時間加上隨機噪音,在條件1中加分得分。 它作為典型的心理學設置具有指導意義。 你有一個任務,通過練習(時間)和提高分數的葯物(條件== 1),人們的分數會變得更好。
為了討論的目的,這里有一些更現實的數據。 現在模擬的參與者具有隨機的“技能”級別,該級別被添加到他們的分數中。 此外,因素現在是字符串。
myDat = structure(list(Score = c(1.62, 2.18, 2.3, 3.46, 3.85, 4.7, 1.41,
2.21, 3.32, 2.73, 3.34, 3.27, 2.14, 2.73, 2.74, 3.39, 3.59, 4.01,
1.81, 1.83, 3.22, 3.64, 3.51, 4.26), Subject = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L,
6L, 7L, 7L, 7L, 8L, 8L, 8L), .Label = c("A", "B", "C", "D", "E",
"F", "G", "H"), class = "factor"), Condition = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("No", "Yes"), class = "factor"),
Time = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("1PM",
"2PM", "3PM"), class = "factor")), .Names = c("Score", "Subject",
"Condition", "Time"), class = "data.frame", row.names = c(NA,
-24L))
看見:
library(ggplot2)
qplot(Time, Score, data = myDat, geom = "line", group = Subject, colour = factor(Condition))
使用nlme庫...
回答您提出的問題,您可以使用以下代碼創建隨機的intecept混合效果模型:
> library(nlme)
> m1 <- lme(Score ~ Condition + Time + Condition*Time,
+ data = myDat, random = ~ 1 | Subject)
> summary(m1)
Linear mixed-effects model fit by REML
Data: myDat
AIC BIC logLik
31.69207 37.66646 -9.846036
Random effects:
Formula: ~1 | Subject
(Intercept) Residual
StdDev: 5.214638e-06 0.3151035
Fixed effects: Score ~ Condition + Time + Condition * Time
Value Std.Error DF t-value p-value
(Intercept) 0.6208333 0.2406643 14 2.579666 0.0218
Condition 0.7841667 0.3403507 6 2.303996 0.0608
Time 0.9900000 0.1114059 14 8.886423 0.0000
Condition:Time 0.0637500 0.1575517 14 0.404629 0.6919
Correlation:
(Intr) Condtn Time
Condition -0.707
Time -0.926 0.655
Condition:Time 0.655 -0.926 -0.707
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-1.5748794 -0.6704147 0.2069426 0.7467785 1.5153752
Number of Observations: 24
Number of Groups: 8
截距方差基本上為0,表示在主題效果內沒有,因此該模型不能很好地捕捉時間關系。 隨機攔截模型很少是您想要重復測量設計的模型類型。 隨機截距模型假設所有時間點之間的相關性相等。 即時間1和時間2之間的相關性與時間1和時間3之間的相關性。在正常情況下(可能不是那些產生假數據的情況),我們預計后者會比前者少。 自動回歸結構通常是更好的方法。
> m2<-gls(Score ~ Condition + Time + Condition*Time,
+ data = myDat, correlation = corAR1(form = ~ Time | Subject))
> summary(m2)
Generalized least squares fit by REML
Model: Score ~ Condition + Time + Condition * Time
Data: myDat
AIC BIC logLik
25.45446 31.42886 -6.727232
Correlation Structure: AR(1)
Formula: ~Time | Subject
Parameter estimate(s):
Phi
-0.5957973
Coefficients:
Value Std.Error t-value p-value
(Intercept) 0.6045402 0.1762743 3.429543 0.0027
Condition 0.8058448 0.2492895 3.232566 0.0042
Time 0.9900000 0.0845312 11.711652 0.0000
Condition:Time 0.0637500 0.1195452 0.533271 0.5997
Correlation:
(Intr) Condtn Time
Condition -0.707
Time -0.959 0.678
Condition:Time 0.678 -0.959 -0.707
Standardized residuals:
Min Q1 Med Q3 Max
-1.6850557 -0.6730898 0.2373639 0.8269703 1.5858942
Residual standard error: 0.2976964
Degrees of freedom: 24 total; 20 residual
您的數據在時間點相關性之間顯示-.596,這似乎很奇怪。 通常情況下,至少應該是時間點之間的正相關。 這些數據是如何產生的?
附錄:
使用您的新數據,我們知道數據生成過程相當於隨機攔截模型(盡管這對於縱向研究來說並不是最真實的。可視化顯示時間的影響似乎是相當線性的,因此我們應該感到舒適將其視為數字變量。
> library(nlme)
> m1 <- lme(Score ~ Condition + as.numeric(Time) + Condition*as.numeric(Time),
+ data = myDat, random = ~ 1 | Subject)
> summary(m1)
Linear mixed-effects model fit by REML
Data: myDat
AIC BIC logLik
38.15055 44.12494 -13.07527
Random effects:
Formula: ~1 | Subject
(Intercept) Residual
StdDev: 0.2457355 0.3173421
Fixed effects: Score ~ Condition + as.numeric(Time) + Condition * as.numeric(Time)
Value Std.Error DF t-value p-value
(Intercept) 1.142500 0.2717382 14 4.204415 0.0009
ConditionYes 1.748333 0.3842958 6 4.549447 0.0039
as.numeric(Time) 0.575000 0.1121974 14 5.124898 0.0002
ConditionYes:as.numeric(Time) -0.197500 0.1586710 14 -1.244714 0.2337
Correlation:
(Intr) CndtnY as.(T)
ConditionYes -0.707
as.numeric(Time) -0.826 0.584
ConditionYes:as.numeric(Time) 0.584 -0.826 -0.707
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-1.44560367 -0.65018585 0.01864079 0.52930925 1.40824838
Number of Observations: 24
Number of Groups: 8
我們看到一個顯着的條件效應,表明'是'條件傾向於具有更高的分數(大約1.7),並且具有顯着的時間效應,表明兩個組隨着時間的推移而上升。 支持該情節,我們發現兩組之間沒有時間差異(相互作用)。 即斜坡是相同的。
這不是您的問題的答案,但您可能會發現您的數據可視化信息。
library(ggplot2)
qplot(Time, Score, data = myDat, geom = "line",
group = Subject, colour = factor(Condition))
(使用lme4庫)這適合您的主題效果為隨機,以及隨機效果分組的變量。 在該模型中,隨機效應是截距隨受試者而變化。
m <- lmer( Score ~ Condition + Time + (1|Subject), data=myDat )
要查看您可以使用的隨機效果
ranef(m)
正如Ian Fellows所提到的,您的數據也可能包含隨機的條件和時間組件。 您可以使用其他模型進行測試。 在下面的一個條件中,時間和截距允許按主題隨機變化。 它還評估了它們之間的相關性。
mi <- lmer( Score ~ Condition + Time + (Condition + Time|Subject), data=myDat )
並嘗試
summary(mi)
ranef(mi)
你也可以在沒有與截距相關的情況下進行測試,條件和時間之間的相互作用,以及許多其他模型,以查看最適合您的數據和/或理論的模型。 你的問題有點模糊,但這幾個命令應該讓你開始。
請注意,主題是您的分組因子,因此您可以將其他效果視為隨機效果。 這並不是你明確適合作為預測者的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.