R 在数值变量中绘制具有交互作用的二项式glm
我想知道是否有可能绘制二项glm与数字变量的相互作用。就我而言:R 在数值变量中绘制具有交互作用的二项式glm,r,glm,binomial-coefficients,R,Glm,Binomial Coefficients,我想知道是否有可能绘制二项glm与数字变量的相互作用。就我而言: ##Data set artificial set.seed(20) d <- data.frame( mating=sample(0:1, 200, replace=T), behv = scale(rpois(200,10)), condition = scale(rnorm(200,5)) ) #Binomial GLM ajusted model<-glm(mating ~ behv
##Data set artificial
set.seed(20)
d <- data.frame(
mating=sample(0:1, 200, replace=T),
behv = scale(rpois(200,10)),
condition = scale(rnorm(200,5))
)
#Binomial GLM ajusted
model<-glm(mating ~ behv + condition, data=d, family=binomial)
summary(model)
##数据集人工
种子集(20)
d看起来您想要的输出是条件平均值(或最佳拟合线)的绘图。您可以通过使用predict
函数计算预测值来实现这一点
我要稍微改变一下你的例子,以得到一个更好的结果
d$mating <- ifelse(d$behv > 0, rbinom(200, 1, .8), rbinom(200, 1, .2))
model <- glm(mating ~ behv + condition, data = d, family = binomial)
summary(model)
最后,我们通过x轴变量对newdata
进行排序(如果不是,我们将得到在绘图上呈之字形的线条),然后绘图:
newdata <- newdata[order(newdata$behv), ]
plot(newdata$mating ~ newdata$behv)
lines(x = newdata$behv, y = newdata$yhat)
newdata
newdata <- d
newdata$condition <- mean(newdata$condition)
newdata$yhat <- predict(model, newdata, type = "response")
newdata <- newdata[order(newdata$behv), ]
plot(newdata$mating ~ newdata$behv)
lines(x = newdata$behv, y = newdata$yhat)