从数据/系数创建 lm 对象

发布于 2024-08-17 23:56:24 字数 499 浏览 7 评论 0原文

有谁知道可以在给定数据集和系数的情况下创建 lm 对象的函数?

我对此很感兴趣,因为我开始使用贝叶斯模型平均 (BMA),并且我希望能够根据 bicreg 的结果创建 lm 对象。我想访问所有不错的通用 lm 函数,例如诊断绘图、预测、cv.lm 等。

如果您非常确定这样的函数不存在,那么了解这一点也非常有帮助!

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)

Does anyone know of a function that can create an lm object given a dataset and coefficients?

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. I'd like to have access to all of the nice generic lm functions like diagnostic plotting, predict, cv.lm etc.

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一片旧的回忆 2024-08-24 23:56:24

据我所知,没有任何函数可以做到这一点。当然可以制作一个。 magicFunction 需要做的就是创建一个包含元素的列表:

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

然后将其设为 lm 对象

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

让我说一下,我认为这是一个坏主意。谁说您应用的通用函数将适用于 bicreg 对象。例如,您如何解释 AIC(fakeModel)?

您最好创建自己的函数来进行诊断和预测。

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:

> 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

> 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. For example, how would you interpret AIC(fakeModel)?

You are better off creating your own functions to do diagnostics and prediction.

澜川若宁 2024-08-24 23:56:24

看来您可以像往常一样计算 lm 对象,然后通过修改 lm() 结果的 $coefficients 属性来修改系数。

有关更多详细信息,请参阅此问题和结果:

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

不确定它是否符合你想要做的事情,但......

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.

See this question and results for more details :

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

Not sure it corresponds to what you want to do, though...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文