模型包含多项式时的新预测
New predictions when model contains a poly term
我正在尝试手动预测 y
新的数据向量 x_new
。 "by hand" 我的意思是不使用 predict
函数(我的实际模型是一个 mcmc
对象,predict
不接受)。这对于像这样的简单模型很好:
lm1 <- lm(Petal.Width ~ Petal.Length + Sepal.Width + Sepal.Length, data=iris)
x_new <- c(1, 1.4, 3.2, 5.2)
y <- x_new %*% lm1$coef
但是当我的模型看起来像这样时,我不确定如何继续:
lm2 <- lm(Petal.Width ~ Petal.Length + Sepal.Width + poly(Sepal.Length,3), data=iris)
如何使用 poly()
变量中的参数?
您需要设置 poly(., raw=TRUE)
以便它使用原始多项式而不是正交多项式。比较,现在它们产生相同的系数:
lm2 <- lm(Petal.Width ~ Petal.Length + Sepal.Width + Sepal.Length +
I(Sepal.Length^2) + I(Sepal.Length^3), data=iris)
lm3 <- lm(Petal.Width ~ Petal.Length + poly(Sepal.Length, 3, raw=TRUE),
data=iris)
> coef(lm2)
(Intercept) Petal.Length Sepal.Width Sepal.Length I(Sepal.Length^2) I(Sepal.Length^3)
10.22126962 0.50889848 0.22999328 -5.81536464 0.98349473 -0.05626378
> coef(lm3)
(Intercept) Petal.Length Sepal.Width poly(Sepal.Length, 3, raw = TRUE)1
10.22126962 0.50889848 0.22999328 -5.81536464
poly(Sepal.Length, 3, raw = TRUE)2 poly(Sepal.Length, 3, raw = TRUE)3
0.98349473 -0.05626378
我正在尝试手动预测 y
新的数据向量 x_new
。 "by hand" 我的意思是不使用 predict
函数(我的实际模型是一个 mcmc
对象,predict
不接受)。这对于像这样的简单模型很好:
lm1 <- lm(Petal.Width ~ Petal.Length + Sepal.Width + Sepal.Length, data=iris)
x_new <- c(1, 1.4, 3.2, 5.2)
y <- x_new %*% lm1$coef
但是当我的模型看起来像这样时,我不确定如何继续:
lm2 <- lm(Petal.Width ~ Petal.Length + Sepal.Width + poly(Sepal.Length,3), data=iris)
如何使用 poly()
变量中的参数?
您需要设置 poly(., raw=TRUE)
以便它使用原始多项式而不是正交多项式。比较,现在它们产生相同的系数:
lm2 <- lm(Petal.Width ~ Petal.Length + Sepal.Width + Sepal.Length +
I(Sepal.Length^2) + I(Sepal.Length^3), data=iris)
lm3 <- lm(Petal.Width ~ Petal.Length + poly(Sepal.Length, 3, raw=TRUE),
data=iris)
> coef(lm2)
(Intercept) Petal.Length Sepal.Width Sepal.Length I(Sepal.Length^2) I(Sepal.Length^3)
10.22126962 0.50889848 0.22999328 -5.81536464 0.98349473 -0.05626378
> coef(lm3)
(Intercept) Petal.Length Sepal.Width poly(Sepal.Length, 3, raw = TRUE)1
10.22126962 0.50889848 0.22999328 -5.81536464
poly(Sepal.Length, 3, raw = TRUE)2 poly(Sepal.Length, 3, raw = TRUE)3
0.98349473 -0.05626378