Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在数值变量中绘制具有交互作用的二项式glm_R_Glm_Binomial Coefficients - Fatal编程技术网

R 在数值变量中绘制具有交互作用的二项式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

我想知道是否有可能绘制二项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 + 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)