# SB1.1 Applied Statistics Non-Assessed Practical Normal Linear Models
######################################################################
#sample answer to Treees data example question
#load the trees data into R
data(trees)
#view the data - str(), head() and summary() also useful
trees
#'trees' is a 'data frame', a kind of matrix in R
#inspect the data - plot does something sensible with a data frame
plot(trees)
#we can apply log() to all the dat and replot - notice the more even distribution
plot(log(trees))
#fit the normal linear model Volume~1+Girth+Height for the trees data.
#the column names of a data frame like trees are the variable names.
trees.lm <- lm(Volume~Girth+Height,data=trees)
#the fitted model has been saved into 'trees.lm'
#we can run other functions on trees.lm to extract information about the fit
summary(trees.lm)
#and store their results in the same way as we did for lm()
trees.sum <- summary(trees.lm)
#lets check the t-test result for 'Height'. trees.sum is a list
#of results each stored under a different name
names(trees.sum)
#we can access the elements in the 'Coefficients' block of the summary()
#output and compute the t-test statistic (beta-hat_3)/std.err(beta-hat_3)
htt <- trees.sum$coefficients[3,1]/trees.sum$coefficients[3,2]
htt
#check that 'htt' is the same number as the t-test statistic for girth
#in the summary() output. Now compute the p-value. Note that 'pt(q,dof)'
#computes Pr(T