R预测不会产生正确的长度

问题描述 投票:0回答:1

我有一个简单的阳性/阴性测试结果,它是蛋白质浓度的函数,我正在尝试绘制logit回归曲线。但是,我始终遇到无法修复的错误。我相信这是一个简单的问题,但是我已经被困了几个小时了。

这是我的数据(感谢第一批回复)

sp <- structure(list(Cry = c(32, 32, 32, 32, 32, 32, 16, 16, 16, 8, 
8, 8, 4, 4, 4, 2, 2, 2, 1, 1, 1, 0.5, 0.5, 0.5, 0.25, 0.25, 0.25, 
0.12, 0.12, 0.06, 0, 0, 0, 0, 0, 0), Positive = c(1L, 1L, 1L, 
1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L)), class = "data.frame", row.names = c(NA, -36L))

这是我的意思:

test.glm <- glm(Positive ~ Cry, family = binomial, data = sp)
summary(test.glm)

试图绘制预测值以作图

x_test <- seq(0,32, 0.01)
y_test <- predict(test.glm, newdata =  list(x_test) ,type = "response")

所以,当我尝试绘制它时:

plot(sp$Positive ~ log(Cry))
lines(x_test, y_test)

我收到此错误:xy.coords(x,y)中的错误:“ x”和“ y”的长度不同

因为

length(x_test)
3201
length(y_test)
36

y_test的长度最终为36,而不是与x_test相同的长度

为什么我的预测函数会忽略新数据的长度,而只预测与原始数据帧中一样多的值?

谢谢!

r glm predict
1个回答
1
投票

我不确定您如何通过该代码获取任何数据,但是也许这正是您想要的?

test.glm <- glm(Positive ~ Cry, family = binomial, data = sp)
x_test <- seq(0,32,0.01)
y_text <- predict(test.glm,newdata = data.frame(Cry = x_test),type="response")
plot(sp, Positive ~ log(Cry))
lines(x_test,y_test)

enter image description here

数据

sp <- structure(list(Cry = c(32, 32, 32, 32, 32, 32, 16, 16, 16, 8, 
8, 8, 4, 4, 4, 2, 2, 2, 1, 1, 1, 0.5, 0.5, 0.5, 0.25, 0.25, 0.25, 
0.12, 0.12, 0.06, 0, 0, 0, 0, 0, 0), Positive = c(1L, 1L, 1L, 
1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L)), class = "data.frame", row.names = c(NA, -36L))
© www.soinside.com 2019 - 2024. All rights reserved.