【发布时间】:2020-12-30 21:35:48
【问题描述】:
从 caret 包中拉出 finalModel 时,我遇到了一个烦人的问题。我需要来自 crossCV 的最终逻辑回归,但列表对象在需要在公式中评估的模型项周围添加了“符号”(例如,“I(x^2)”)。这会使该对象无法用于任何其他包。
我尝试过:1. 在函数调用中声明模型公式,2. 使用 poly() 而不是 I 作为术语,3. 在模型对象上使用 gsub() 函数替换字符的 lapply, 4. 在模型对象上使用 grep 和 lappy 只是为了找到字符并使用 gsub 手动通过。但基本上,lapply 不会深入挖掘所有可变列表长度,也不会返回模型对象。
#Here's the problem
library(caret)
dat=data.frame(y=as.factor(rbinom(1000, 1, prob=0.5)),
x=rnorm(1000,10,1),
w=rnorm(1000,100,1),
z=rnorm(1000,1000,1))
levels(dat$y) <- c("A","P")
Train <- createDataPartition(dat$y, p=0.7, list=FALSE)
train<- dat[ Train, ]
test <- dat[ -Train, ]
lof=as.formula(y~ x+I(x^2)+w+I(w^2)+z+I(z^2))
m1<-glm(lof,family="binomial", data=train)
m1
pred1=predict(m1,newdata=test, type="response" )
ctrl <- trainControl(
method = "repeatedcv",
repeats = 3,
classProbs = TRUE,
summaryFunction = twoClassSummary,
)
m2<-train(lof,family="binomial", data=train,
method="glm",
trControl = ctrl,
metric = "ROC")
m3=m2$finalModel
m3
pred2=predict(m3,newdata=test, type="response" )
res1=lapply(m3, function (x) grepl('\\`I',x))
m3$terms[[3]]=gsub('\\`I',"",m3$terms[[3]])
m3$terms[[3]]=gsub(')\\`',"",m3$terms[[3]])
m3$terms
res=lapply(m3, function (x) grepl('\\`I',names(x)))
names(m3$effects)[res$effects==TRUE]=gsub('\\`I',"",names(m3$effects)[res$effects==TRUE])
names(m3$effects)[res$effects==TRUE]=gsub(')\\`',"",names(m3$effects)[res$effects==TRUE])
pred3=predict(m3,newdata=test, type="response" )
尝试使用 lapply 修复会发现一些实例,但不是全部,并且不能轻易用于修改原始对象。因此,即使是手动修复也受到阻碍。显然,最简单的解决方案是首先弄清楚如何阻止插入符号执行此操作,而不是尝试编辑对象。
我可能应该注意,我并没有尝试提取 finalModel 以与 predict 一起使用,我知道我可以在整个 m2 对象上使用插入符号进行预测...我希望它与 dismo 包一起使用。
【问题讨论】: