简体   繁体   English

从数据/系数创建lm对象

[英]Create lm object from data/coefficients

Does anyone know of a function that can create an lm object given a dataset and coefficients? 有没有人知道在给定数据集和系数的情况下可以创建lm对象的函数?

I'm interested in this because I started playing with Bayesian model averaging (BMA) and I'd like to be able to create an lm object out of the results of bicreg. 我对此很感兴趣,因为我开始玩贝叶斯模型平均(BMA),我希望能够从bicreg的结果中创建一个lm对象。 I'd like to have access to all of the nice generic lm functions like diagnostic plotting, predict, cv.lm etc. 我想访问所有漂亮的通用lm函数,如诊断绘图,预测,cv.lm等。

If you are pretty sure such a function doesn't exist that's also very helpful to know! 如果您非常确定不存在这样的功能,那么知道它也会非常有用!

library(BMA)
mtcars_y <- mtcars[, 1] #mpg
mtcars_x <- as.matrix(mtcars[,-1])
res <- bicreg(mtcars_x, mtcars_y)

summary(res)
res$postmean # bma coefficients

# The approximate form of the function
# I'm looking for
lmObject <- magicFunction(data=mtcars, coefficients=res$postmean)

There is no function that I am aware of that does this. 没有我知道的功能就是这样做。 One could of course be made. 当然可以制作一个。 All that your magicFunction would need to do is create a list with elements: 你的magicFunction需要做的就是创建一个包含元素的列表:

> names(fakeModel)
[1] "coefficients"  "residuals"     "effects"       "rank"         
 [5] "fitted.values" "assign"        "qr"            "df.residual"  
 [9] "xlevels"       "call"          "terms"         "model"  

then make it an lm object 然后把它变成一个lm对象

> class(fakeModel) <- c("lm")

Let me just say that I think that this is a bad idea though. 我只想说,我认为这是一个坏主意。 Whose to say that the generic function that you apply will be applicable to a bicreg object. 可以这么说,您应用的泛型函数将适用于bicreg对象。 For example, how would you interpret AIC(fakeModel)? 例如,您如何解释AIC(fakeModel)?

You are better off creating your own functions to do diagnostics and prediction. 您最好创建自己的功能来进行诊断和预测。

It seems you can compute your lm object as usual, and then modify the coefficients afterwards by modifying the $coefficients attribute of your lm() result. 看起来你可以照常计算你的lm对象,然后通过修改你的lm()结果的$coefficients属性来修改$coefficients

See this question and results for more details : 有关详细信息,请参阅此问题和结果:

http://tolstoy.newcastle.edu.au/R/e2/help/07/08/24294.html http://tolstoy.newcastle.edu.au/R/e2/help/07/08/24294.html

Not sure it corresponds to what you want to do, though... 不确定它对应于你想做什么,不过......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM