具有任意系数 r 的 predict.lm [英] predict.lm with arbitrary coefficients r
问题描述
我正在尝试使用 predict.lm 来预测 lm 对象.但是,我想使用手动插入的系数.为此,我尝试过:
I'm trying to predict an lm object using predict.lm. However, I would like to use manually inserted coefficients. To do this I tried:
model$coefficients <- coeff
(其中coeff"是正确系数的向量)这确实会根据我的需要修改系数.尽管如此,当我执行
(where "coeff" is a vector of correct coefficients) which would indeed modify the coefficients as I want. Nevertheless, when I execute
predict.lm(model, new.data)
我只是得到使用旧"参数计算的预测.有没有办法强制 predict.lm 使用新的?
I just get predictions calculated with the "old" parameters. Is there a way I could force predict.lm to use the new ones?
Post Scriptum:我需要这样做以适应 bin-smooth(也称为回归图).此外,当我手动"预测(即使用矩阵乘法)时,结果很好,因此我很确定问题在于 predict.lm 无法识别我的新系数.
Post Scriptum: I need to do this to fit a bin-smooth (also called regressogram). In addition, when I predict "by hand" (i.e. using matrix multiplication) the results are fine, hence I'm quite sure that the problem lies in the predict.lm not recognizing my new coefficients.
预先感谢您的帮助!
推荐答案
破解 $coefficients
元素似乎确实有效.你能说明什么对你不起作用吗?
Hacking the $coefficients
element does indeed seem to work. Can you show what doesn't work for you?
dd <- data.frame(x=1:5,y=1:5)
m1 <- lm(y~x,dd)
m1$coefficients <- c(-2,1)
m1
## Call:
## lm(formula = y ~ x, data = dd)
##
## Coefficients:
## [1] -2 1
predict(m1,newdata=data.frame(x=7)) ## 5 = -2+1*7
predict.lm(...)
给出相同的结果.
我会非常谨慎地使用这种方法,每次你对被黑模型做不同的事情时都会检查.
I would be very careful with this approach, checking each time you do something different with the hacked model.
一般来说,如果 predict
和 simulate
方法接受一个 newparams
参数会很好,但它们通常不会......
In general it would be nice if predict
and simulate
methods took a newparams
argument, but they don't in general ...
这篇关于具有任意系数 r 的 predict.lm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!