我正在使用cake資料集并構建了以下模型
mod <- lmer(angle ~ recipe*temp (1|replicate), data=cake)
newdat <- expand.grid(recipe=unique(cake$recipe),
temp=c(min(cake$temp),
max(cake$temp)))
然后我想replicate為每個recipe(A和)分別繪制回歸線BC
ggplot(cake, aes(x=temp, y=angle, color=recipe))
geom_line(aes(y=predict(mod), group=replicate, size="Replicate"))
geom_line(data=newdat, aes(y=predict(mod, re.form=~0, newdata=newdat), size="Population"))
scale_size_manual(name="Predictions", values=c("Replicate"=0.5, "Population"=1))
我有兩個問題。首先,似乎Population線B和C不平行,即使我沒有定義隨機斜率模型。replicate其次,我不能讓上面的代碼繪制每個分割的回歸線recipe。
uj5u.com熱心網友回復:
要回答您的第一個問題,您的行并不平行,因為您的公式之間recipe和temp公式中有一個互動項。因此,對于固定效應,您有不同的斜率(即,angle每單位變化的變化temp取決于recipe)。即使我們繪制一個簡單的圖,我們也可以看到這一點lm:
library(ggplot2)
mod <- lm(angle ~ recipe * temp, data = cake)
newdat <- expand.grid(recipe=unique(cake$recipe),
temp=c(min(cake$temp),
max(cake$temp)))
newdat$angle <- predict(mod, newdata = newdat)
ggplot(cake, aes(temp, angle, color = recipe))
geom_point(position = position_jitter(width = 1))
geom_line(data = newdat)
scale_y_continuous(limits = c(25, 40))

要回答第二個問題,您需要按相互作用進行分組,replicate并recipe為每個復制品獲得正確顏色的線條。您還需要為它們單獨lm生成行。最簡單的方法是通過geom_smoothwith method = lm:
ggplot(cake, aes(temp, angle, color = recipe))
geom_point(position = position_jitter(width = 1))
geom_smooth(aes(group = interaction(replicate, recipe)), se = FALSE, method = lm)

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/431945.html
