### Task 1
seedl<-read.delim2("clipboard")
summary(seedl)
lm.0<-lm(seedlings~+1, data=seedl)
summary(lm.0)
add1(lm.0, ~+treatment*productivity*temperature, test="F")
# Single term additions
# 
# Model:
# seedlings ~ +1
#              Df Sum of Sq    RSS     AIC F value    Pr(>F)    
# <none>                    819.37 101.220                      
# treatment     2    400.47 418.90  85.093 12.9059 0.0001166 ***
# productivity  1     67.35 752.01 100.647  2.5078 0.1245162    
# temperature   1     39.55 779.81 101.736  1.4203 0.2433658    
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
lm.1<-update(lm.0, .~.+treatment)
add1(lm.1, ~+treatment*productivity*temperature, test="F")
# Single term additions
# 
# Model:
# seedlings ~ treatment
#              Df Sum of Sq    RSS    AIC F value  Pr(>F)  
# <none>                    418.90 85.093                  
# productivity  1    49.148 369.75 83.349  3.4560 0.07438 .
# temperature   1    45.090 373.81 83.677  3.1362 0.08830 .
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

lm.2<-update(lm.1, .~.+productivity)
add1(lm.2, ~+treatment*productivity*temperature, test="F")
lm.3<-update(lm.2, .~.+treatment:productivity)
summary(lm.3)


plot(lm.3)
# Call:
# lm(formula = seedlings ~ treatment + productivity + treatment:productivity, 
#     data = seedl)
# 
# Residuals:
#     Min      1Q  Median      3Q     Max 
# -7.4156 -2.0861 -0.0552  2.3954  5.5553 
# 
# Coefficients:
#                             Estimate Std. Error t value Pr(>|t|)  
# (Intercept)                  9.00124    6.28992   1.431   0.1653  
# treatmentmown1               9.89010    7.42960   1.331   0.1956  
# treatmentmown2               8.84194    7.41663   1.192   0.2448  
# productivity                 0.02099    0.01354   1.550   0.1342  
# treatmentmown1:productivity -0.03669    0.01546  -2.373   0.0260 *
# treatmentmown2:productivity -0.03686    0.01598  -2.306   0.0301 *
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 3.494 on 24 degrees of freedom
# Multiple R-squared:  0.6425,	Adjusted R-squared:  0.568 
# F-statistic: 8.626 on 5 and 24 DF,  p-value: 8.675e-05

###Task 2
transpi<-read.delim2("clipboard")
summary(transpi)
lm.0<-lm(transpiration~+1, data=transpi)
add1(lm.0, .~.+wind+temperature+humidity+sunshine, test="F")
# Model:
# transpiration ~ +1
#             Df Sum of Sq     RSS    AIC F value   Pr(>F)   
# <none>                   158.000 35.930                    
# wind         1    75.369  82.631 28.855 10.9454 0.006243 **
# temperature  1    93.763  64.237 25.329 17.5157 0.001265 **
# humidity     1     0.038 157.962 37.926  0.0029 0.958179   
# sunshine     1     4.667 153.333 37.510  0.3652 0.556876  

lm.1<-update(lm.0, .~.+temperature)
add1(lm.1, .~.+wind+temperature+humidity+sunshine, test="F")
lm.2<-update(lm.1, .~.+wind)
add1(lm.2, .~.+wind+temperature+humidity+sunshine, test="F")
summary(lm.2)
# Call:
# lm(formula = transpiration ~ temperature + wind, data = transpi)
# 
# Residuals:
#     Min      1Q  Median      3Q     Max 
# -2.8698 -0.7002 -0.2190  0.8018  2.2732 
# 
# Coefficients:
#             Estimate Std. Error t value Pr(>|t|)    
# (Intercept)  5.45464    1.14007   4.784 0.000567 ***
# temperature  0.38619    0.08048   4.798 0.000555 ***
# wind         0.67796    0.17247   3.931 0.002349 ** 
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 1.558 on 11 degrees of freedom
# Multiple R-squared:  0.8309,	Adjusted R-squared:  0.8002 
# F-statistic: 27.03 on 2 and 11 DF,  p-value: 5.681e-05

plot(lm.2)

###Task 3
plants<-read.delim2("clipboard")
summary(plants)
lm.1<-lm(log(biomass)~watering*fertil, data=plants)
drop1(lm.1, test="F")
plot(lm.1)

summary(lm.1)
# Call:
# lm(formula = log(biomass) ~ watering * fertil, data = plants)
# 
# Residuals:
#       Min        1Q    Median        3Q       Max 
# -0.215320 -0.065523  0.009066  0.081658  0.213493 
# 
# Coefficients:
#                                  Estimate Std. Error t value Pr(>|t|)    
# (Intercept)                       4.36385    0.05277  82.695  < 2e-16 ***
# wateringWatered                   0.07558    0.07463   1.013  0.32328    
# fertilFertilized                  0.24639    0.07463   3.301  0.00356 ** 
# wateringWatered:fertilFertilized  0.37369    0.10554   3.541  0.00205 ** 
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 0.1293 on 20 degrees of freedom
# Multiple R-squared:  0.8396,	Adjusted R-squared:  0.8155 
# F-statistic: 34.89 on 3 and 20 DF,  p-value: 3.862e-08

###Taks 4
people<-read.delim2("clipboard")
summary(people)
lm.0<-lm(mass~+1, data=people)
add1(lm.0, ~+height+sex+colour+vegetarian+hours, test="F")
lm.1<-update(lm.0, .~.+height)
add1(lm.1, ~+height+sex+colour+vegetarian+hours, test="F")
lm.2<-update(lm.1, .~.+sex)
add1(lm.2, ~+height+sex+colour+vegetarian+hours, test="F")
lm.3<-update(lm.2, .~.+hours)
add1(lm.3, ~+height+sex+colour+vegetarian+hours, test="F")
anova(lm.2, lm.3)
# 
# summary(lm.2)
# Call:
# lm(formula = mass ~ height + sex, data = people)
# 
# Residuals:
#    Min     1Q Median     3Q    Max 
# -8.559 -5.865 -2.027  3.041 20.865 
# 
# Coefficients:
#             Estimate Std. Error t value Pr(>|t|)    
# (Intercept) -41.8184    28.9782  -1.443 0.166170    
# height        0.6062     0.1697   3.572 0.002180 ** 
# sexM         16.9264     4.1986   4.031 0.000783 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 7.876 on 18 degrees of freedom
# Multiple R-squared:  0.7901,	Adjusted R-squared:  0.7668 
# F-statistic: 33.87 on 2 and 18 DF,  p-value: 7.914e-07

anova(lm.0, lm.1, lm.2)

5318.7-1116.5

lm.1a<-update(lm.0, .~.+sex)

anova(lm.0, lm.1a, lm.2)

t.test(people$height~people$sex)
