【问题标题】:Plotting binomial glm with interactions in numeric variables绘制具有数字变量交互作用的二项式 glm
【发布时间】:2017-02-07 19:39:27
【问题描述】:

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

在行为和条件在模型中很重要的情况下

#Plotting first for behv
x<-d$behv ###Take behv values
x2<-rep(mean(d$condition),length(d_p[,1])) ##Fixed mean condition

# Points
plot(d$mating~d$behv)

#Curve
curve(exp(model$coefficients[1]+model$coefficients[2]*x+model$coefficients[3]*x2)
/(1+exp(model$coefficients[1]+model$coefficients[2]*x+model$coefficients[3]*x2)))

但是不行!!还有另一种正确的做法吗?

谢谢

【问题讨论】:

    标签: r glm binomial-coefficients


    【解决方案1】:

    您想要的输出似乎是条件均值(或最佳拟合线)的图。您可以通过使用 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)
    

    现在,我们使用您想要的值创建一个 newdata 数据框:

    newdata <- d
    newdata$condition <- mean(newdata$condition)
    newdata$yhat <- predict(model, newdata, type = "response")
    

    最后,我们按 x 轴变量对newdata 进行排序(如果没有,我们将得到在整个绘图上呈锯齿形的线条),然后绘制:

    newdata <- newdata[order(newdata$behv), ]
    plot(newdata$mating ~ newdata$behv)
    lines(x = newdata$behv, y = newdata$yhat)
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      相关资源
      最近更新 更多