【问题标题】:Predict error for GLMs in R with number of rows用行数预测 R 中 GLM 的误差
【发布时间】:2020-09-12 09:40:56
【问题描述】:

我正在尝试获取 R 中逻辑 glm 的预测曲线。我创建了一个新数据框,但不明白为什么我不断收到此错误。感谢您的帮助!

glm1 <- glm(dataset_presence$presence ~ dataset_presence$Int_Bk, data = dataset_presence, family = binomial, na.action = na.exclude)

newdat <- data.frame(Int_Bk = seq(min(dataset_presence$Int_Bk), max(dataset_presence$Int_Bk), length=50))

newdat$presence <- predict(glm1, newdata = newdat, type="response")

$&lt;-.data.frame(*tmp*, 存在, value = c(0.862135653229272, : 替换有 167 行,数据有 50 另外:警告信息: 'newdata' 有 50 行,但找到的变量有 167 行

【问题讨论】:

    标签: r glm predict


    【解决方案1】:

    您需要在公式中指定不带data$variable的型号:

    glm1 <- glm(presence ~ Int_Bk, data = dataset_presence, family = binomial, na.action = na.exclude)
    

    一旦你这样做了,你的预测就会奏效。也就是说,如果你想绘制曲线,ggeffectseffects 包有一些非常有用的功能。例如,使用carData 包中的Chile 数据:

    library(ggeffects)
    data("Chile", package="carData")
    
    Chile <- Chile %>% 
      filter(vote %in% c("Y", "N"))
    m2 <- glm(vote ~ age, data=Chile, family=binomial)
    
    p <- ggpredict(m2, terms="age")
    plot(p)
    

    ggpredict 对象的plot() 方法会生成一个ggplot

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      相关资源
      最近更新 更多