问题描述
我正在尝试获取R中逻辑Logm的预测曲线。我创建了一个新的数据框,但不明白为什么我一直收到此错误。谢谢您的帮助!
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")
$<-.data.frame
(*tmP*
中的错误,存在,值= c(0.862135653229272,: 替换有167行,数据有50行 另外:警告消息: “ newdata”有50行,但找到的变量有167行
解决方法
您需要在公式中指定不含data$variable
的模型:
glm1 <- glm(presence ~ Int_Bk,data = dataset_presence,family = binomial,na.action = na.exclude)
执行此操作后,您的预测将起作用。就是说,如果您想绘制曲线,则ggeffects
和effects
包具有一些非常有用的功能。例如,使用Chile
包中的carData
数据:
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)
plot()
对象的ggpredict
方法产生一个ggplot
。