내용 |
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)
fit.origin <- lm(y~poly(x,2,raw=TRUE))
y <- c(99.5,104.8,60.5,100,86,64,35.3,15) #3번째 원래 값은 108
xx <- seq(30,160, length=50)
plot(x, y, pch=19, ylim=c(0,150))
lines(xx, predict(fit.origin, data.frame(x=xx)), col="red", lty="dashed")
fit.lm <- lm(y~poly(x,2,raw=TRUE))
lines(xx, predict(fit.lm, data.frame(x=xx)), col="black")
#install.packages("quantreg")
library("quantreg")
fit.rq <- rq(y ~ poly(x,2,raw=TRUE))
lines(xx, predict(fit.rq, data.frame(x=xx)), col="blue")
library("MASS")
fit.rlm <- rlm(y ~ poly(x,2,raw=TRUE))
lines(xx, predict(fit.rlm, data.frame(x=xx)), col="purple")
legend("topright", inset=0.05, bty="n",
legend = c("lm fit to original data", "lm fit", "rq fit", "rlm fit"),
lty = c(2, 1, 1, 1), # 1 = "solid" ; 2 = "dashed"
col = c("red", "black", "blue", "purple")
)
|