r- 如何将 boxtidwell 的结果存储到数据中 frame/matrix

r- how to store the result of boxtidwell into a data frame/matrix

我想存储boxtidwell的结果,以便编写一些代码来实现自动化,避免手动转换变量。请参见下面的示例:

 >boxTidwell(prestige ~ income + education, ~ type + poly(women, 2), data = Prestige) 

 ##            Score Statistic   p-value MLE of lambda
 ## income          -4.482406 0.0000074    -0.3476283
 ## education        0.216991 0.8282154     1.2538274 
 ## iterations =  8

因为我不想手动转换变量,例如下面的代码:

 >Prestige$income <- (Prestige$income )^(-0.3476283)

当需要转换的变量很多的时候,会很费时间。我尝试了 matrix 和 data.frame 但都没有用。

> box<-boxTidwell(prestige ~ income + education, ~ type + poly(women, 2),   data = Prestige)
> box<-as.data.frame(box)

#Error in as.data.frame.default(box) : 
#cannot coerce class ""boxTidwell"" to a data.frame

> box<-as.matrix(box)
> box

#Error in round(x$result, digits) : 
#non-numeric argument to mathematical function

我已经搜索了一段时间但似乎是徒劳的,非常感谢您提前提出任何建议和想法。

它可以在 box$result 中访问,例如

box$result[,"MLE of lambda"]

    income  education 
-0.3476283  1.2538274 

所以你上面的代码变成了(粗略地)
Prestige$income <- (Prestige$income )^box$result[1,3]

一般来说,要查看对象内部的内容,请使用 str