Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当新数据的级别较少时,R predict.glm_R_Regression_Glm_Predict - Fatal编程技术网

当新数据的级别较少时,R predict.glm

当新数据的级别较少时,R predict.glm,r,regression,glm,predict,R,Regression,Glm,Predict,我试图向自己证明,当newdata的标签和级别(因子级别的基本整数)与列车数据的标签和级别不匹配时,predict()不会给出错误的预测 我想我确实证明了这一点,我在下面分享了这段代码,但我想问一下,当预测新数据时,R到底在做什么。我知道它不是将newdata附加到训练数据,它是否在预测之前将newdata的因子标签转换为训练数据的相应表示 options(stringsAsFactors = TRUE) dat <- data.frame(x = rep(c("cat", "dog",

我试图向自己证明,当
newdata
的标签和级别(因子级别的基本整数)与列车数据的标签和级别不匹配时,
predict()
不会给出错误的预测

我想我确实证明了这一点,我在下面分享了这段代码,但我想问一下,当预测
新数据时,R到底在做什么。我知道它不是将
newdata
附加到训练数据,它是否在预测之前将
newdata
的因子标签转换为训练数据的相应表示

options(stringsAsFactors = TRUE)
dat <- data.frame(x = rep(c("cat", "dog", "bird", "horse"), 100), y = rgamma(100, shape=3,  scale = 300))
model <- glm(y~., family = Gamma(link = "log"), data = dat)

coefficients(model)
# (Intercept)        xcat        xdog      xhorse 
#   6.5816536   0.2924488   0.3586094   0.2740487 

newdata1 <- data.frame(x = "cat")
newdata2 <- data.frame(x = "bird")
newdata3 <- data.frame(x = "dog")

predict.glm(object = model, newdata = newdata1, type = "response")
#       1 
# 966.907 
exp(6.5816536 + 0.2924488) #intercept + cat coef
# [1] 966.9071

predict.glm(object = model, newdata = newdata2, type = "response")
#        1 
# 721.7318 
exp(6.5816536)
# [1] 721.7318

predict.glm(object = model, newdata = newdata3, type = "response")
#        1 
# 1033.042 
exp(6.5816536 + 0.3586094)
# [1] 1033.042

unclass(dat$x)
#  [1] 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3
# [87] 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4
# [173] 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3
# [259] 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4
# [345] 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4
# attr(,"levels")
# [1] "bird"  "cat"   "dog"   "horse"

unclass(newdata1$x)
# [1] 1
# attr(,"levels")
# [1] "cat"

unclass(newdata2$x)
# [1] 1
# attr(,"levels")
# [1] "bird"
选项(stringsAsFactors=TRUE)

dat模型对象具有用于模型估计的
xlevels
记录因子级别。例如,我们有:

model$xlevels
#$x
#[1] "bird"  "cat"   "dog"   "horse"

当您的新数据显示在预测中时,因子级别将匹配。例如,您的
newdata1
将与“cat”级别匹配,这是
xlevels
中的第二个级别。因此,predict将很容易找到该级别的正确系数。

模型对象具有用于模型估计的
xlevels
记录因子级别。例如,我们有:

model$xlevels
#$x
#[1] "bird"  "cat"   "dog"   "horse"

当您的新数据显示在预测中时,因子级别将匹配。例如,您的
newdata1
将与“cat”级别匹配,这是
xlevels
中的第二个级别。因此,predict将很容易找到该级别的正确系数。

谢谢-无法找到如何接受答案,但这是我需要的。谢谢-无法找到如何接受答案,但这是我需要的。