Skip to content

Instantly share code, notes, and snippets.

@dragstar328
Created March 20, 2015 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dragstar328/6aa55e02325bb219e24c to your computer and use it in GitHub Desktop.
Save dragstar328/6aa55e02325bb219e24c to your computer and use it in GitHub Desktop.
Regression of cars
cars
plot(cars)
cars.lm = lm(dist~.,cars)
summary(cars.lm)
# dist = 3.03*speed - 17.58
plot(cars)
abline(cars.lm, col="red")
#予測区間を求める
cars.pre = predict(cars.lm, interval = "prediction")
#下側
lines(cars$speed, cars.pre[,2], col="blue", lty=2)
#上側
lines(cars$speed, cars.pre[,3], col="blue", lty=2)
#信頼区間を求める
cars.con = predict(cars.lm, interval = "confidence")
#下側
lines(cars$speed, cars.con[,2], col="green", lty=1)
#上側
lines(cars$speed, cars.con[,3], col="green", lty=1)
x = function(speed) {
dist = 3.93 * speed - 17.58
return (dist)
}
x(40)
#標準誤差
se = 6.7584
#誤差率
seper = (se / mean(cars$dist))*100 #15%
pointx = 23
kaiki = x(pointx)
kaiki.low = kaiki * (1-0.15)
kaiki.high = kaiki * (1+0.15)
kaiki.x = c(23,23)
kaiki.y = c(kaiki.low, kaiki.high)
lines(kaiki.x, kaiki.y, type="l" ,lwd = 5,col="yellow")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment