# Tasainen regressio library(MASS) library(splines) # Additiiviset mallit ja tasoituskäyrät data(mcycle) attach(mcycle) par(mfrow = c(3,2)) plot(times, accel, main="Polynomial regression") lines(times, fitted(lm(accel ~ poly(times, 3)))) lines(times, fitted(lm(accel ~ poly(times, 6))), lty=3) legend(40, -100, c("degree=3", "degree=6"), lty=c(1,3),bty="n") library(splines) plot(times, accel, main="Natural splines") lines(times, fitted(lm(accel ~ ns(times, df=5)))) lines(times, fitted(lm(accel ~ ns(times, df=10))), lty=3) lines(times, fitted(lm(accel ~ ns(times, df=20))), lty=4) legend(40, -100, c("df=5", "df=10", "df=20"), lty=c(1,3,4), bty="n") # B-splines #plot(times, accel, main="B-splines") #lines(times, fitted(lm(accel ~ bs(times, df=5)))) #lines(times, fitted(lm(accel ~ bs(times, df=10))), lty=3) #lines(times, fitted(lm(accel ~ bs(times, df=20))), lty=4) #legend(40, -100, c("df=5", "df=10", "df=20"), lty=c(1,3,4), # bty="n") library(modreg) plot(times, accel, main="Smoothing splines") lines(smooth.spline(times, accel)) plot(times, accel, main="Lowess") lines(lowess(times, accel)) lines(lowess(times, accel, 0.2), lty=3) legend(40, -100, c("default span", "f = 0.2"), lty=c(1,3), bty="n") plot(times, accel, main ="ksmooth") lines(ksmooth(times, accel,"normal", bandwidth=5)) lines(ksmooth(times, accel,"normal", bandwidth=2), lty=3) legend(40, -100, c("bandwidth=5", "bandwidth=2"), lty=c(1,3), bty="n") plot(times, accel, main ="supsmu") lines(supsmu(times, accel)) lines(supsmu(times, accel, bass=3), lty=3) legend(40, -100, c("default", "bass=3"), lty=c(1,3), bty="n") #################### # Splinit # Runkokäyrä #################### # Polynomisovitus ################## y1 <- scan() 357 345 337 326 314 301 299 297 286 281 278 276 270 268 268 266 266 265 261 260 256 252 250 250 245 242 241 238 234 234 227 217 217 214 212 211 203 202 198 196 182 168 163 147 143 132 124 113 110 105 x1<- seq(0, 14.7, by=0.3) y1 <- y1/10 plot(x1,y1) lines(x1, fitted(lm(y1 ~ poly(x1, 3)))) lines(x1, fitted(lm(y1 ~ poly(x1, 2)))) ################# ########### # B-Splinit ########### plot(times, accel, main="B-splines") lines(times, fitted(lm(accel ~ bs(times,df=1,degree=1)))) lines(times, fitted(lm(accel ~ bs(times,df=2,degree=1)))) lines(times, fitted(lm(accel ~ bs(times,df=5,degree=1)))) lines(times, fitted(lm(accel ~ bs(times,df=20,degree=1)))) # lines(times, fitted(lm(accel ~ bs(times,knots=30,degree=1)))) lines(times, fitted(lm(accel ~ bs(times,knots=c(30,40),degree=1)))) # x <- 1:10 y <- x^2 plot(x,y) lines(x, fitted(lm(y ~ bs(x,knots=6,degree=1)))) # dg1.s <- lm(y ~ bs(x,knots=6,degree=1)) names(dg1.s) summary(dg1.s) # x <- 1:10 y <- x^2-2*log(x) plot(x,y) lines(x, fitted(lm(y ~ bs(x,knots=6,degree=3)))) # # kuutiollinen splini y <- 5*x/(1+x) plot(x,y) lines(x, fitted(lm(y ~ bs(x,knots=6,degree=3)))) # # bs():n ja ns():n ero # plot(times, accel, main="Natural splines") lines(times, fitted(lm(accel ~ ns(times, df=5)))) lines(times, fitted(lm(accel ~ bs(times, df=5))),lty=2) ################### ################### y <- c(1,2.5,5,4,2,2.1,5,7,11,11) plot(x,y) lines(x, fitted(lm(y ~ bs(x,knots=c(3,6),degree=1)))) splin1 <- lm(y ~ bs(x,knots=c(3,6),degree=1)) splin1 #splin2 <- lm(y ~ bs(x,df=3,degree=1)) #lines(x, fitted(splin2)) ################### y1 <- scan() 357 345 337 326 314 301 299 297 286 281 278 276 270 268 268 266 266 265 261 260 256 252 250 250 245 242 241 238 234 234 227 217 217 214 212 211 203 202 198 196 182 168 163 147 143 132 124 113 110 105 x1<- seq(0, 14.7, by=0.3) y1 <- y1/10 plot(x1,y1) lines(x1, fitted(lm(y1 ~ bs(x1,df=3,degree=1)))) lines(x1, fitted(lm(y1 ~ bs(x1,knots=c(2.5,10),degree=1)))) lines(x1, fitted(lm(y1 ~ bs(x1,knots=6,degree=3)))) #################### ##################### # TASOITUSSPLINIT ##################### library(modreg) #smooth.spline(x, y, w = rep(1, length(x)), df = 5, spar = 0, # cv = FALSE, all.knots = FALSE, df.offset = 0, penalty = 1) plot(times, accel, main="Smoothing splines") lines(smooth.spline(times, accel)) mcycle.ssp <- smooth.spline(times, accel, penalty = 0) lines(mcycle.ssp) mcycle.ssp <- smooth.spline(times, accel , all.knots = TRUE, penalty = 0) lines(mcycle.ssp) mcycle.ssp <- smooth.spline(times, accel , all.knots = TRUE, penalty = 1) lines(mcycle.ssp) ############# detach()