head(cars)

attach(cars)


# 1 plot


plot(dist~speed, data = cars)


# 2 lm() ~ abline()


m3 <- lm(dist~speed, cars)

abline(m3, col = "red")


# 3 draw the residual line by lines & sapply


yhat <- predict(m3, speed = speed)

cbind( dist, yhat )


join <- function(i)

lines( c(speed[i], speed[i]), c(dist[i], yhat[i]), col="green")

sapply(1:50, join)

반응형
Posted by JoeSung
,