![](/img/trans.png)
[英]Visualising a three way interaction between two continuous variables and one categorical variable in R
[英]Visualising GLMM predictions with interaction of categorical and continuous variables
我正在使用 GLMM 在 R 中工作,該 GLMM 混合了連續和分類變量以及一些交互。 我在 MuMIn 中使用了 dredge 和 model.avg 函數來獲得每個變量的效果估計。 我的問題是如何最好地繪制結果。 我想制作一個圖表,顯示一個變量(森林)對我的數據的影響,其中趨勢線反映了森林參數估計,但我無法弄清楚如何將分類變量和交互變量保持在它們的“平均值”,以便趨勢線僅反映森林的影響。
這是模型和繪圖設置:
#load packages and document
cuckoo<-read.table("http://www.acsu.buffalo.edu/~ciaranwi/home_range.txt",
header=T,sep="\t")
require(lme4)
require(MuMIn)
as.factor (cuckoo$ID)
as.factor (cuckoo$Sex)
as.factor(cuckoo$MS_bin)
options(na.action = "na.fail")
# create global model and fit
fm<- lmer(log(KD_95)~ MS_bin + Forest + NDVI + Sex + Precip + MS_bin*Forest
+ MS_bin*NDVI + MS_bin*Sex + MS_bin*Precip + Argos + Sample + (1|ID), data
= cuckoo, REML = FALSE)
# dredge but always include argos and sample
KD95<-dredge(fm,fixed=c("Argos","Sample"))
# model averaging
avgmod<-model.avg(KD95, fit=TRUE)
summary(avgmod)
#plot data
plot(cuckoo$Forest, (log(cuckoo$KD_95)),
xlab = "Mean percentage of forest cover",
ylab = expression(paste(plain("Log of Kernel density estimate, 95%
utilisation, km"^{2}))),
pch = c(15,17)[as.numeric(cuckoo$MS_bin)],
main = "Forest cover",
col="black",
ylim=c(14,23))
legend(80,22, c("Breeding","Nonbreeding"), pch=c(15, 17), cex=0.7)
然后我陷入了如何包含趨勢線的問題。 到目前為止,我有:
#parameter estimates from model.avg
argos_est<- -1.6
MS_est<- -1.77
samp_est<-0.01
forest_est<--0.02
sex_est<-0.0653
precip_est<-0.0004
ndvi_est<--0.00003
model_intercept<-22.7
#calculate mean values for parameters
argos_mean<-mean(cuckoo$Argos)
samp_mean<-mean(cuckoo$Sample)
forest_mean<-mean(cuckoo$Forest)
ndvi_mean<-mean(cuckoo$NDVI)
precip_mean<-mean(cuckoo$Precip)
#calculate the intercept and add trend line
intercept<-(model_intercept + (forest_est*cuckoo$Forest) +
(argos_est*argos_mean) + (samp_est * samp_mean) + (ndvi_est*ndvi_mean) +
(precip_est*precip_mean) )
abline(intercept, forest_est)
但這並沒有考慮交互作用或分類變量,截距看起來太高了。 有任何想法嗎?
在流程方面,您可以利用 R 在模型對象中存儲大量有關模型的信息並具有從模型對象中獲取信息的功能這一事實,從而使您的編碼變得更加容易。 例如, coef(avgmod)
將為您提供模型系數,而predict(avgmod)
將為您提供用於擬合模型的數據框中每個觀察值的模型預測。
為了可視化我們感興趣的數據值的特定組合的預測,創建一個新的數據框,其中包含我們想要保持不變的變量的均值,以及我們想要改變的變量值的范圍(如Forest
) . expand.grid
使用下面列出的值的所有組合創建一個數據框。
pred.data = expand.grid(Argos=mean(cuckoo$Argos), Sample=mean(cuckoo$Sample),
Precip=mean(cuckoo$Precip), NDVI=mean(cuckoo$NDVI),
Sex="M", Forest=seq(0,100,10), MS_bin=unique(cuckoo$MS_bin),
ID=unique(cuckoo$ID))
現在我們使用predict
函數將 log(KD_95) 的predict
添加到這個數據幀。 predict
負責計算您提供給它的任何數據的模型預測(假設您給它一個包含模型中所有變量的數據框)。
pred.data$lgKD_95_pred = predict(avgmod, newdata=pred.data)
現在我們繪制結果。 geom_point
繪制點,就像在原始圖中一樣,然后geom_line
為MS_bin
每個級別(和 Sex="M")添加預測。
library(ggplot2)
ggplot() +
geom_point(data=cuckoo, aes(Forest, log(KD_95), shape=factor(MS_bin),
colour=factor(MS_bin), size=3)) +
geom_line(data=pred.data, aes(Forest, lKD_95_pred, colour=factor(MS_bin)))
結果如下:
更新:要繪制男性和女性的回歸線,只需在pred.data
包含 Sex="F" 並在pred.data
添加Sex
作為美學。 在下面的示例中,我在繪制點時使用不同的形狀來標記Sex
,並使用不同的線型來標記回歸線的Sex
。
pred.data = expand.grid(Argos=mean(cuckoo$Argos), Sample=mean(cuckoo$Sample),
Precip=mean(cuckoo$Precip), NDVI=mean(cuckoo$NDVI),
Sex=unique(cuckoo$Sex), Forest=seq(0,100,10), MS_bin=unique(cuckoo$MS_bin),
ID=unique(cuckoo$ID))
pred.data$lgKD_95_pred = predict(avgmod, newdata=pred.data)
ggplot() +
geom_point(data=cuckoo, aes(Forest, log(KD_95), shape=Sex,
colour=factor(MS_bin)), size=3) +
geom_line(data=pred.data, aes(Forest, lgKD_95_pred, linetype=Sex,
colour=factor(MS_bin)))
我希望我沒有錯過這一點,但如果你想要一個線性趨勢,你實際上不必手動計算所有內容,而是獲取你繪制的內容並擬合 a~x 線性回歸模型,如下所示:
model = lm(log(cuckoo$KD_95)~cuckoo$Forest)
model
# Call:
# lm(formula = log(cuckoo$KD_95) ~ cuckoo$Forest)
#
# Coefficients:
# (Intercept) cuckoo$Forest
# 17.13698 -0.01461
abline(17.13698 , -0.01461, col="red")
紅線使用回歸擬合的截距和斜率。 黑線是您的手動過程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.