###Practicals 9
###Task 1
snow<-data.frame(temp=c(-18, -20,-5,-12, -8,-9,-2,-14, -8,-17, -2,-4,-3,-12),
  diam=c(4.9,6.1,8.3, 8,8.7,8.5,10.2,7.1,8.4, 6.2,9.9,9.3,11,7.6))

summary(snow)

lm.1<-lm(diam~temp, data=snow)## Fitting the regression model
plot(lm.1)# regression diagnostics. Certain departure from
#normal distribution may be observed. However, that's based just on two observations
# I would consider this still acceptable with some reservatrions
anova(lm.1) # Anova test of significance
# Analysis of Variance Table
# 
# Response: diam
#           Df Sum Sq Mean Sq F value    Pr(>F)    
# temp       1 32.798  32.798  89.111 6.652e-07 ***
# Residuals 12  4.417   0.368                      
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


summary(lm.1)# Summary of the model
# 
# Call:
# lm(formula = diam ~ temp, data = snow)
# 
# Residuals:
#      Min       1Q   Median       3Q      Max 
# -1.05721 -0.21684  0.07169  0.17846  1.12766 
# 
# Coefficients:
#             Estimate Std. Error t value Pr(>|t|)    
# (Intercept) 10.65537    0.31037   34.33 2.37e-13 ***
# temp         0.26101    0.02765    9.44 6.65e-07 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 0.6067 on 12 degrees of freedom
# Multiple R-squared:  0.8813,	Adjusted R-squared:  0.8714 
# F-statistic: 89.11 on 1 and 12 DF,  p-value: 6.652e-07

#Conclusion: yes, snowflake size  significantly positively depends
#on temperature. 



#Plotting
plot(diam~temp, data=snow)
abline(coef(lm.1)) # Plot of the regression line

# If you want to plot also the confidence intervals
# then it is necessary to generate model prediction over
# the interval required (paramter newdata in function predict below)
pred<-predict(lm.1, newdata = data.frame(temp=seq(-21, 0, by=0.1)),
              se=T) # Generates the prediction, se=T specifies that 
# standard error of the estimate should be generated
pred$df# contains degrees of freedom of the model. That is needed
# for computation of CIs from t-distribution and SE

lines(seq(-21, 0, by=0.1), pred$fit)# adds the regression line itself
lines(seq(-21, 0, by=0.1), pred$fit+pred$se.fit*qt(0.975,pred$df),
      lty=2)# adds the lower confidence interval
lines(seq(-21, 0, by=0.1), pred$fit+pred$se.fit*qt(0.025,pred$df),
      lty=2)# adds the upper confidence interval


# For the 5°C question, you may use this
predict(lm.1, newdata = data.frame(temp=5))
#        1 
# 11.96042 

# But usually, there is no snow at such air temperature. This
# also point to the general problem of extrapollation of the prediction out of the 
# original data range, which should be avoided

#Alternatively, you can use the lmconf function provided in
# IS - that's easy but you do not learn much
lmconf(diam~temp, data=snow)


## Task 2
ministry<-read.delim("clipboard")
summary(ministry)
 #       IQ              beer      
 # Min.   : 65.00   Min.   :0.500  
 # 1st Qu.: 81.75   1st Qu.:1.075  
 # Median : 92.50   Median :2.150  
 # Mean   : 90.33   Mean   :3.033  
 # 3rd Qu.: 99.75   3rd Qu.:3.775  
 # Max.   :111.00   Max.   :8.000 
cor.test(ministry$IQ,ministry$beer)
# 
# 	Pearson's product-moment correlation
# 
# data:  ministry$IQ and ministry$beer
# t = -0.51757, df = 10, p-value = 0.616
# alternative hypothesis: true correlation is not equal to 0
# 95 percent confidence interval:
#  -0.6730350  0.4545116
# sample estimates:
#        cor 
# -0.1615223 

## Conclusion: There is no significant association between 
# beer consumption and the IQ of ministry employees.
