###Practicals 9
library(readxl)
###Task 1
snow<-read_excel("09_data.xlsx", sheet="1")
  
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 reservations
anova(lm.1) # Anova test of significance
# Analysis of Variance Table
# 
# Response: diam
#           Df  Sum Sq Mean Sq F value    Pr(>F)    
# temp       1 18.3649  18.365   88.73 6.806e-07 ***
# Residuals 12  2.4837   0.207                      
# ---
# 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 
# -0.72142 -0.24955  0.06217  0.19966  0.80825 
# 
# Coefficients:
#             Estimate Std. Error t value Pr(>|t|)    
# (Intercept) 10.19798    0.23274   43.82 1.30e-14 ***
# temp         0.19531    0.02073    9.42 6.81e-07 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 0.4549 on 12 degrees of freedom
# Multiple R-squared:  0.8809,	Adjusted R-squared:  0.8709 
# F-statistic: 88.73 on 1 and 12 DF,  p-value: 6.806e-07


#Conclusion: Crystal temperature has a  significant positive effect on snowflake size.

#Plotting
library(ggplot2)
ggplot(snow, aes(x=temp, y=diam))+geom_point()+geom_smooth(method="lm",color=1)+
  theme_classic()+labs(x="Temperature", y="Snowflake diameter")


# 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 of such temperature. This
# also point to the general problem of extrapolation of the prediction out of the 
# original data range, which should be avoided


## Task 2
ministry<-read_excel("09_data.xlsx", sheet="2")
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.

ggplot(ministry, aes(x=beer, y=IQ))+geom_point()+
  theme_classic()+labs(x="Beer consumed daily (liters)", y="IQ")

## Task 3 

school<-read_excel("09_data.xlsx", sheet="3")
  

lm.1<-lm(speed~height, data=school)#Fitting the regression model

plot(lm.1) #Regression diagnostics plots

anova(lm.1) # Significance test
# Analysis of Variance Table
# 
# Response: speed
#            Df  Sum Sq Mean Sq F value  Pr(>F)  
# height      1   1.514 1.51393   6.241 0.01279 *
# Residuals 528 128.082 0.24258                  
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

summary(lm.1)  # Summary of the model
# Call:
# lm(formula = speed ~ height, data = school)
# 
# Residuals:
#      Min       1Q   Median       3Q      Max 
# -1.38749 -0.34697  0.00673  0.36496  1.72864 
# 
# Coefficients:
#             Estimate Std. Error t value Pr(>|t|)  
# (Intercept) -4.23057    2.54571  -1.662   0.0971 .
# height       0.05299    0.02121   2.498   0.0128 *
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 0.4925 on 528 degrees of freedom
# Multiple R-squared:  0.01168,	Adjusted R-squared:  0.00981 
# F-statistic: 6.241 on 1 and 528 DF,  p-value: 0.01279

# Conclusion: Height has a significant effect on swimming speed.
# The explained variability is very small though. There are probably some other factors, 
# which may have stronger effect on the speed of swimming.


ggplot(school, aes(x=height, y=speed))+geom_point()+geom_smooth(method="lm", color=1)+
  theme_classic()+labs(x="Height(cm)", y="Swimming speed (km/h)")

