我目前正在尝试使用lm()
将多项式模型拟合到测量数据。
fit_poly4 <- lm(y ~ poly(x, degree = 4, raw = T), weights = w)
以x
为独立,y
为因变量,w
= 1 /测量方差。
我想尝试一个给定系数的多项式而不是R确定的多项式。具体来说我想要我的多项式
y = -3,3583*x^4 + 43*x^3 - 191,14*x^2 + 328,2*x - 137,7
我试着输入它
fit_poly4 <- lm(y ~ 328.2*x-191.14*I(x^2)+43*I(x^3)-3.3583*I(x^4)-137.3,
weights = w)
但这只会返回一个错误:
terms.formula(formula,data = data)中的错误:ExtractVars中的模型公式无效
有没有办法确定lm()
中的系数以及如何做到这一点?
我不确定你为什么要这样做,但是你可以使用一个偏移项:
set.seed(101)
dd <- data.frame(x=rnorm(1000),y=rnorm(1000), w = rlnorm(1000))
fit_poly4 <- lm(y ~
-1 + offset(328.2*x-191.14*I(x^2)+43*I(x^3)-3.3583*I(x^4)-137.3),
data=dd,
weights = w)
-1
抑制了通常的拦截术语。