【发布时间】:2019-04-09 23:29:33
【问题描述】:
我需要绘制 x 和 y 之间的关系,其中 x 的多项式预测 y。这是使用 poly() 函数完成的,以确保多项式是正交的。
如何同时考虑线性、二次和三次项来绘制这种关系?问题是不同项的系数没有像 x 那样缩放。
我在下面提供了一些示例代码。我尝试将每个多项式的对比度值重新分配给 x。
这个解决方案给出了不可能的预测值。
提前感谢您的帮助!
最好的祝愿, 埃里克
这是一个示例代码:
x = sample(0:6,100,replace = TRUE)
y = (x*0.2) + (x^2*.05) + (x^3*0.001)
y = y + rnorm(100)
x = poly(x,3)
m = lm(y~x)
TAB = summary(m)$coefficients
### Reassigning the corresponding contrast values to each polynomial of x:
eq = function(x,TAB,start) {
#argument 'start' is used to determine the position of the linear coefficient, quadratic and cubic follow
pols = poly(x,3)
x1=pols[,1]; x2=pols[,2]; x3=pols[,3]
TAB[1,1] + x1[x]*TAB[start,1] + x2[x] * TAB[start+1,1] + x3[x] * TAB[start+2,1]
}
plot(eq(0:7,TAB,2))
【问题讨论】:
-
我通常通过
predict()获取预测值来解决此类问题。它本质上是一个方便的功能,可以做(我认为)你想做的事情。