R语言 为什么glm模型会收敛,而相同模型的插入字符串版本不会?

jgwigjjp  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(165)

据我所知,我已经使用基本的glm函数和插入字符串函数以相同的方式指定了这个简单的GLM,但是插入字符串版本不会收敛,我指定的train模型是否缺少什么?

library(caret)

logo <- rast(system.file("ex/logo.tif", package="terra"))   
names(logo) <- c("red", "green", "blue")
p <- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85, 
              66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38, 31, 
              22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2)

a <- matrix(c(22, 33, 64, 85, 92, 94, 59, 27, 30, 64, 60, 33, 31, 9,
              99, 67, 15, 5, 4, 30, 8, 37, 42, 27, 19, 69, 60, 73, 3, 5, 21,
              37, 52, 70, 74, 9, 13, 4, 17, 47), ncol=2)



xy <- rbind(cbind(1, p), cbind(0, a))

# extract predictor values for points
e <- terra::extract(logo, xy[,2:3])

# combine with response (excluding the ID column)
v <- data.frame(cbind(pa=xy[,1], e))
v$pa <- as.factor(v$pa)

#GLM model

model <- glm(formula=as.numeric(pa)~ red + blue + green , data=v)

#Train model

model2 <- train(pa ~ red + green + blue, 
               data=v,
               method = "glm")

>Warning messages:
> 1: glm.fit: algorithm did not converge
> 2: glm.fit: fitted probabilities numerically 0 or 1 occurred
mrzz3bfm

mrzz3bfm1#

@Ben Bolker在评论中的赞美:
训练模型使用family=“二项式”作为默认值,因为响应变量为0和1。训练模型使用family=“高斯”。

相关问题