rm(list=ls())
ls()
sapply(c(10,20,30,60,300), function(x) qt(p=0.975, df=x))
qnorm(p=0.975)
install.packages("AER")
help("StockWatson2007", package = "AER")
data("CASchools", package = "AER")
str(CASchools)
CASchools$stratio <- with(CASchools, students/teachers)
CASchools$score <- with(CASchools, (math + read)/2)
library(car)
plot(score ~ stratio, data=CASchools, xlim=c(0,max(CASchools$stratio)))
lm1 <- lm(score ~ stratio, data = CASchools)
summary(lm1)
abline(lm1, col="green")
quantile(CASchools$stratio, probs=seq(0,1,.1))
quantile(CASchools$score, probs=seq(0,1,.1))
sqrt(1/(420-2) * sum(lm1$resid^2))
str(lm1)
mean(CASchools$score)
lm(score ~ 1, data=CASchools)
RSS <- sum(lm1$resid^2)
ESS <- sum( (lm1$fitted - mean(lm1$fitted))^2 )
TSS <- RSS + ESS
ESS/TSS
scatterplot(lm1$resid ~ CASchools$stratio)
e = 2 + rnorm(1000)
x = runif(1000,0,10)
y = 0 + x + e
plot(y ~ x, main="Include a constant in the regression")
abline( lm(y ~ x), col="blue", lwd=2)
abline( lm(y ~ -1 + x), col="red", lwd=2)
legend("topleft",legend=c("y = a + bx","y = bx"),lwd=2,lty=1,col=c("blue","red"))
par(mfrow=c(1,2))
hist(lm1$resid, 20, freq=FALSE)
lines(dnorm(seq(-50,50,.1),sd=18.58) ~ seq(-50,50,.1), col="blue")
qqnorm(lm1$resid)
qqline(lm1$resid)
set.seed(12)
x = rt(150,10)
hist(x, 20, freq=FALSE, main="Histrogram of t-distribution with 10 d.f.")
lines(dnorm(seq(-6,6,.1),sd=sd(x)) ~ seq(-6,6,.1), col="blue")
qqnorm(x)
qqline(x)