识别 R 中非线性回归的拟合方程
Identifying the fitted equation of a non-linear regression in R
这是来自Dave Tang's Blog on curve fitting
的代码
x <- c(32,64,96,118,126,144,152.5,158)
y <- c(99.5,104.8,108.5,100,86,64,35.3,15)
plot(x,y,pch=19)
fit <- lm(y~poly(x,4,raw=TRUE))
summary(fit)
Call:lm(formula = y ~ poly(x, 4, raw = TRUE))
Residuals:
1 2 3 4 5 6 7 8
0.1242 -0.6912 1.6355 1.4491 -5.1240 4.0360 -0.4692 -0.9604
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.474e+01 5.473e+01 1.366 0.265
poly(x, 4, raw = TRUE)1 1.426e+00 3.095e+00 0.461 0.676
poly(x, 4, raw = TRUE)2 -2.854e-02 5.729e-02 -0.498 0.653
poly(x, 4, raw = TRUE)3 2.878e-04 4.278e-04 0.673 0.549
poly(x, 4, raw = TRUE)4 -1.134e-06 1.113e-06 -1.018 0.384
Residual standard error: 4.04 on 3 degrees of freedom
Multiple R-squared: 0.9943, Adjusted R-squared: 0.9868
F-statistic: 131.5 on 4 and 3 DF, p-value: 0.001064
鉴于我们认为这是一个很好的拟合,我想知道拟合的确切多项式方程是什么。有什么办法可以实现吗?
[编辑]
另外一个问题,我看到 p 值都倾向于表明自变量不够显着,但我们看到一个很好的拟合,有人可以解释一下吗
您在摘要中有系数:
f <- function(x) {
return(7.473766e+01 + 1.425813e+00*x -2.854370e-02*x^2 + 2.877714e-04*x^3 - 1.133744e-06*x^4 )
}
plot(x, y)
lines(x, f(x), col="red")
您可以使用程序包 polynom
中的函数 polynomial
来编写等式:
library(polynom)
x <- c(32,64,96,118,126,144,152.5,158)
y <- c(99.5,104.8,108.5,100,86,64,35.3,15)
plot(x,y,pch=19)
fit <- lm(y~poly(x,4,raw=TRUE))
p0 <- polynomial(coef(fit))
p0
# 74.73766 + 1.425813*x - 0.0285437*x^2 + 0.0002877714*x^3 - 1.133744e-06*x^4
使用signif
四舍五入系数:
p0 <- polynomial(signif(coef(fit), 3))
p0
# 74.7 + 1.43*x - 0.0285*x^2 + 0.000288*x^3 - 1.13e-06*x^4
你可以和p0
一起玩一下:
f0 <- as.function(p0)
f0(x)
# [1] 99.37580 105.49117 106.86449 98.55089 91.12402 59.96402 35.76922
# [8] 15.96039
predict(fit)
# 1 2 3 4 5 6 7 8
# 99.37580 105.49117 106.86449 98.55089 91.12402 59.96402 35.76922 15.96039
plot(x, y)
lines(x, f0(x), col = "grey", lwd = 2) # bold grey line
lines(x, predict(fit), col = "red", lty = 2) # dashed red line
这是来自Dave Tang's Blog on curve fitting
的代码x <- c(32,64,96,118,126,144,152.5,158)
y <- c(99.5,104.8,108.5,100,86,64,35.3,15)
plot(x,y,pch=19)
fit <- lm(y~poly(x,4,raw=TRUE))
summary(fit)
Call:lm(formula = y ~ poly(x, 4, raw = TRUE))
Residuals:
1 2 3 4 5 6 7 8
0.1242 -0.6912 1.6355 1.4491 -5.1240 4.0360 -0.4692 -0.9604
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.474e+01 5.473e+01 1.366 0.265
poly(x, 4, raw = TRUE)1 1.426e+00 3.095e+00 0.461 0.676
poly(x, 4, raw = TRUE)2 -2.854e-02 5.729e-02 -0.498 0.653
poly(x, 4, raw = TRUE)3 2.878e-04 4.278e-04 0.673 0.549
poly(x, 4, raw = TRUE)4 -1.134e-06 1.113e-06 -1.018 0.384
Residual standard error: 4.04 on 3 degrees of freedom
Multiple R-squared: 0.9943, Adjusted R-squared: 0.9868
F-statistic: 131.5 on 4 and 3 DF, p-value: 0.001064
鉴于我们认为这是一个很好的拟合,我想知道拟合的确切多项式方程是什么。有什么办法可以实现吗?
[编辑]
另外一个问题,我看到 p 值都倾向于表明自变量不够显着,但我们看到一个很好的拟合,有人可以解释一下吗
您在摘要中有系数:
f <- function(x) {
return(7.473766e+01 + 1.425813e+00*x -2.854370e-02*x^2 + 2.877714e-04*x^3 - 1.133744e-06*x^4 )
}
plot(x, y)
lines(x, f(x), col="red")
您可以使用程序包 polynom
中的函数 polynomial
来编写等式:
library(polynom)
x <- c(32,64,96,118,126,144,152.5,158)
y <- c(99.5,104.8,108.5,100,86,64,35.3,15)
plot(x,y,pch=19)
fit <- lm(y~poly(x,4,raw=TRUE))
p0 <- polynomial(coef(fit))
p0
# 74.73766 + 1.425813*x - 0.0285437*x^2 + 0.0002877714*x^3 - 1.133744e-06*x^4
使用signif
四舍五入系数:
p0 <- polynomial(signif(coef(fit), 3))
p0
# 74.7 + 1.43*x - 0.0285*x^2 + 0.000288*x^3 - 1.13e-06*x^4
你可以和p0
一起玩一下:
f0 <- as.function(p0)
f0(x)
# [1] 99.37580 105.49117 106.86449 98.55089 91.12402 59.96402 35.76922
# [8] 15.96039
predict(fit)
# 1 2 3 4 5 6 7 8
# 99.37580 105.49117 106.86449 98.55089 91.12402 59.96402 35.76922 15.96039
plot(x, y)
lines(x, f0(x), col = "grey", lwd = 2) # bold grey line
lines(x, predict(fit), col = "red", lty = 2) # dashed red line