为什么 predict.glm() 没有以预期的方式创建预测值?
Why does a predict.glm() not create predicted values in the expected manner?
我正在努力了解 predict.glm() 函数对使用它的工作项目的作用。
为此,我首先查看了在 ?predict.glm() 的文档中找到的示例代码。这让我觉得它可以采用 glm 并预测给定输入向量的响应值。但是我发现很难自定义那个“budworm”示例。所以我自己创建了一个非常简单的模型来尝试看看它是如何工作的。剧透 - 我仍然无法让它工作。
a<-c(1,2,3,4,5)
b<-c(2,3,4,5,6)
result<-glm(b~a,family=gaussian)
summary(result)
plot(c(0,10), c(0,10), type = "n", xlab = "dose",
ylab = "response")
xvals<-seq(0,10,0.1)
data.frame(xinputs=xvals)
predict.glm(object=result,newdata= data.frame(xinputs=xvals),type='terms')
#lines(xvals, predict.glm(object=result,newdata = xvals, type="response" ))
当我 运行 predict.glm(object=result,newdata= data.frame(xinputs=xvals),type='terms')
我收到错误消息:
Warning message:
'newdata' had 101 rows but variables found have 5 rows
据我了解,输入 GLM 仅使用 5 行并不重要......它应该使用该 GLM 的统计数据来预测新数据的 101 个条目中的每一个的响应值?
newdata
数据框中的列名必须与您用于拟合模型的数据中的列名匹配。因此,
predict.glm(object=result,newdata= data.frame(a=xvals),type='terms')
将解决您的问题。
a <- c(1, 2, 3, 4, 5)
b <- c(2, 3, 4, 5, 6)
result <- glm(b ~ a, family = gaussian)
summary(result)
#>
#> Call:
#> glm(formula = b ~ a, family = gaussian)
#>
#> Deviance Residuals:
#> 1 2 3 4 5
#> -1.776e-15 -8.882e-16 -8.882e-16 0.000e+00 0.000e+00
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 1.000e+00 1.317e-15 7.591e+14 <2e-16 ***
#> a 1.000e+00 3.972e-16 2.518e+15 <2e-16 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> (Dispersion parameter for gaussian family taken to be 1.577722e-30)
#>
#> Null deviance: 1.0000e+01 on 4 degrees of freedom
#> Residual deviance: 4.7332e-30 on 3 degrees of freedom
#> AIC: -325.47
#>
#> Number of Fisher Scoring iterations: 1
plot(c(0, 10),
c(0, 10),
type = "n",
xlab = "dose",
ylab = "response")
xvals <- seq(0, 10, 0.1)
head(data.frame(xinputs = xvals))
#> xinputs
#> 1 0.0
#> 2 0.1
#> 3 0.2
#> 4 0.3
#> 5 0.4
#> 6 0.5
head(predict.glm(object = result,
newdata = data.frame(a = xvals),
type = 'terms'))
#> a
#> 1 -3.0
#> 2 -2.9
#> 3 -2.8
#> 4 -2.7
#> 5 -2.6
#> 6 -2.5
由 reprex package (v0.3.0)
于 2020-09-15 创建
我正在努力了解 predict.glm() 函数对使用它的工作项目的作用。
为此,我首先查看了在 ?predict.glm() 的文档中找到的示例代码。这让我觉得它可以采用 glm 并预测给定输入向量的响应值。但是我发现很难自定义那个“budworm”示例。所以我自己创建了一个非常简单的模型来尝试看看它是如何工作的。剧透 - 我仍然无法让它工作。
a<-c(1,2,3,4,5)
b<-c(2,3,4,5,6)
result<-glm(b~a,family=gaussian)
summary(result)
plot(c(0,10), c(0,10), type = "n", xlab = "dose",
ylab = "response")
xvals<-seq(0,10,0.1)
data.frame(xinputs=xvals)
predict.glm(object=result,newdata= data.frame(xinputs=xvals),type='terms')
#lines(xvals, predict.glm(object=result,newdata = xvals, type="response" ))
当我 运行 predict.glm(object=result,newdata= data.frame(xinputs=xvals),type='terms')
我收到错误消息:
Warning message:
'newdata' had 101 rows but variables found have 5 rows
据我了解,输入 GLM 仅使用 5 行并不重要......它应该使用该 GLM 的统计数据来预测新数据的 101 个条目中的每一个的响应值?
newdata
数据框中的列名必须与您用于拟合模型的数据中的列名匹配。因此,
predict.glm(object=result,newdata= data.frame(a=xvals),type='terms')
将解决您的问题。
a <- c(1, 2, 3, 4, 5)
b <- c(2, 3, 4, 5, 6)
result <- glm(b ~ a, family = gaussian)
summary(result)
#>
#> Call:
#> glm(formula = b ~ a, family = gaussian)
#>
#> Deviance Residuals:
#> 1 2 3 4 5
#> -1.776e-15 -8.882e-16 -8.882e-16 0.000e+00 0.000e+00
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 1.000e+00 1.317e-15 7.591e+14 <2e-16 ***
#> a 1.000e+00 3.972e-16 2.518e+15 <2e-16 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> (Dispersion parameter for gaussian family taken to be 1.577722e-30)
#>
#> Null deviance: 1.0000e+01 on 4 degrees of freedom
#> Residual deviance: 4.7332e-30 on 3 degrees of freedom
#> AIC: -325.47
#>
#> Number of Fisher Scoring iterations: 1
plot(c(0, 10),
c(0, 10),
type = "n",
xlab = "dose",
ylab = "response")
xvals <- seq(0, 10, 0.1)
head(data.frame(xinputs = xvals))
#> xinputs
#> 1 0.0
#> 2 0.1
#> 3 0.2
#> 4 0.3
#> 5 0.4
#> 6 0.5
head(predict.glm(object = result,
newdata = data.frame(a = xvals),
type = 'terms'))
#> a
#> 1 -3.0
#> 2 -2.9
#> 3 -2.8
#> 4 -2.7
#> 5 -2.6
#> 6 -2.5
由 reprex package (v0.3.0)
于 2020-09-15 创建