【问题标题】:Format finalModel from caret从插入符号格式化 finalModel
【发布时间】: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 包一起使用。

【问题讨论】:

    标签: r r-caret


    【解决方案1】:

    没有回复,但我最终做的解决方法是创建新变量 x2=x^2 等,就目前而言这很好,但对于我的使用,这意味着我还必须创建新的栅格图层使用 x^2 等是浪费时间和空间。

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 2019-06-02
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      相关资源
      最近更新 更多