# Teht. 1

airpol<-read.table("http://users.jyu.fi/~junyblom/airpol.dat", header=TRUE)
attach(airpol)
malli<-lm(tmr~smean+pmean)
a<-coef(malli)
a
malli2<-lm(tmr~perwh+nonpoor+ge65+smean+pmean)
b<-coef(malli2)
b


# Teht. 6

x <- rnorm(1000, mean =175, sd = 8.3)
y <- rnorm(1000, mean = (175 + 0.8*(x-175)), sd = sqrt((8.3^2)*(1-0.8^2)))
aineisto2 <- data.frame(x,y)


# a)

malli1 <- lm(y ~ x, data=aineisto2)
summary(malli1)
plot(aineisto2$x,aineisto2$y)
abline(coef(malli1))


# b)

osadata <- aineisto2[aineisto2$x >= 170 & aineisto2$x <= 190,]
malli2 <- lm(y ~ x, data=osadata)
summary(malli2)
plot(osadata$x, osadata$y)
abline(coef(malli2))


# c)

res1 <- resid(malli1)
sigma2.hat1 <- sum(res1^2)/malli1$df.residual
sigma.hat1 <- sqrt(sigma2.hat1)

res2 <- resid(malli2)
sigma2.hat2 <- sum(res2^2)/malli2$df.residual
sigma.hat2 <- sqrt(sigma2.hat2)

# 1) Selitysaste laskee huomattavasti
# 2) Kertoimissa ainoastaan vähäistä muutosta (tilastollinen merkitsevyys laskee) 
# 3) Jäännöskeskivirheet lähes samoja (laskevat hieman) 


