我目前正在尝试使用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()
中的系数,并且该怎么做?
答案 0 :(得分:2)
我不确定为什么要这样做,但是可以使用偏移量项:
> dput(cpu[,1:6])
structure(list(Datapoints.Timestamp = structure(1L, .Label = "2019-03-05T08:00:00Z", class = "factor"),
Datapoints.Maximum = 7.83333333332848, Datapoints.Unit = structure(1L, .Label = "Percent", class = "factor"),
Datapoints.Timestamp.1 = structure(1L, .Label = "2019-03-11T22:00:00Z", class = "factor"),
Datapoints.Maximum.1 = 24.2500000000048, Datapoints.Unit.1 = structure(1L, .Label = "Percent", class = "factor")), .Names = c("Datapoints.Timestamp",
"Datapoints.Maximum", "Datapoints.Unit", "Datapoints.Timestamp.1",
"Datapoints.Maximum.1", "Datapoints.Unit.1"), class = "data.frame", row.names = c(NA,
-1L))
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)
取消了通常的拦截项。