根据数据/系数创建lm对象 [英] Create lm object from data/coefficients

查看:100
本文介绍了根据数据/系数创建lm对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道给定数据集和系数的函数可以创建lm对象吗?

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

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

推荐答案

据我所知,没有函数可以做到这一点.当然可以做到.您的magicFunction功能所需要做的就是创建一个包含元素的列表:

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"  

然后将其设为lm对象

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

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

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.

这篇关于根据数据/系数创建lm对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆