Skip to content

Commit

Permalink
fix minor typos
Browse files Browse the repository at this point in the history
  • Loading branch information
alexd106 committed Mar 26, 2024
1 parent cc11b5e commit 396220c
Show file tree
Hide file tree
Showing 18 changed files with 89 additions and 87 deletions.
24 changes: 12 additions & 12 deletions exercise_solutions/graphical_data_exploration_exercise_solutions.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## ----Q4, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q4, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## loyn <- read.table("./data/loyn.txt", header = TRUE,
## stringsAsFactors = TRUE)
## str(loyn)
Expand All @@ -13,14 +13,14 @@
## loyn$FGRAZE <- factor(loyn$GRAZE)


## ----Q5, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q5, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## table(loyn$FGRAZE)
##
## # or use xtabs function
## xtabs(~ FGRAZE, data = loyn)


## ----Q6, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q6, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## # mean abundance of birds for each level of FGRAZE
## tapply(loyn$ABUND, loyn$FGRAZE, mean, na.rm = TRUE)
##
Expand All @@ -31,7 +31,7 @@
## tapply(loyn$ABUND, loyn$FGRAZE, summary, na.rm = TRUE)


## ----Q7a, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE----------------------------------------------------------------------
## ----Q7a, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## # first split the plotting device into 2 rows
## # and 3 columns
## par(mfrow = c(2,3))
Expand All @@ -45,7 +45,7 @@
## dotchart(loyn$GRAZE, main = "Grazing levels")


## ----Q7b, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE----------------------------------------------------------------------
## ----Q7b, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## # A fancier version of a dotplot - just for fun!
## Z <- cbind(loyn$ABUND, loyn$AREA, loyn$DIST,
## loyn$LDIST,loyn$YR.ISOL,loyn$ALT,
Expand All @@ -68,7 +68,7 @@
## ylab = "Order of the data from text file")


## ----Q8, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q8, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## # There appears to be two unusually large forest patches compared to the rest
## # Also one potentially large distance in DIST
## # One option would be to log10 transform AREA, DIST
Expand All @@ -81,7 +81,7 @@
## str(loyn)


## ----Q9a, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE----------------------------------------------------------------------
## ----Q9a, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## # Vanilla pairs plot
##
## pairs(loyn[,c("LOGAREA", "LOGDIST", "LDIST",
Expand All @@ -95,7 +95,7 @@
## pairs(explan_vars)


## ----Q9b, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE----------------------------------------------------------------------
## ----Q9b, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## # And with correlations in the upper panel
##
## # first need to define the panel.cor function
Expand All @@ -115,23 +115,23 @@
## upper.panel = panel.cor)


## ----Q10a, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE---------------------------------------------------------------------
## ----Q10a, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE----------------------------------------------------------------------
## pairs(loyn[,c("ABUND","LOGAREA","LOGDIST", "LDIST",
## "YR.ISOL","ALT","GRAZE")],
## lower.panel = panel.cor)


## ----Q10b, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE---------------------------------------------------------------------
## ----Q10b, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE----------------------------------------------------------------------
## plot(loyn$LOGAREA, loyn$ABUND, xlab = "log area", ylab = "bird abundance")


## ----Q11a, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE---------------------------------------------------------------------
## ----Q11a, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE----------------------------------------------------------------------
## # Interaction between LOGAREA and FGRAZE?
## # Do the slopes look similar or different?
## coplot(ABUND ~ LOGAREA | FGRAZE, data = loyn)


## ----Q11b, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE---------------------------------------------------------------------
## ----Q11b, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE----------------------------------------------------------------------
## # Fancier version of the above plot
## # with a line of best fit included just for fun
## coplot(ABUND ~ LOGAREA | FGRAZE,
Expand Down
24 changes: 12 additions & 12 deletions exercise_solutions/linear_model_1_exercise_solutions.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
## ----Q2, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q2, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## loyn <- read.table("data/loyn.txt", header = TRUE,
## stringsAsFactors = TRUE)
## str(loyn)
##
## # 67 observations and 8 variables (from str())


## ----Q3, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q3, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## # let's first log10 transform the AREA variable due to the couple
## # of unusually large forest area values (check you data exploration)
## # to remind yourself
Expand All @@ -23,7 +23,7 @@
## loyn.lm <- lm(ABUND ~ LOGAREA, data = loyn)


## ----Q4, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q4, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## # ANOVA table
## anova(loyn.lm)
##
Expand All @@ -36,7 +36,7 @@
## # from 0)


## ----Q5, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q5, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## summary(loyn.lm)
##
## # the estimate of the intercept = 10.4
Expand All @@ -46,7 +46,7 @@
## # ABUND = 10.40 + 9.78 * LOGAREA


## ----Q6, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q6, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
##
## # the null hypothesis for the intercept is that the intercept = 0
## # the p value is very small (certainly less than the (not so magic) 0.05)
Expand All @@ -60,14 +60,14 @@
## # LOGAREA and ABUND).


## ----Q7, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q7, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## summary(loyn.lm)
##
## # The multiple R-squared value is 0.588 and therefore 58.8% of
## # the variation in ABUND is explained by LOGAREA


## ----Q8, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q8, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## # first split the plotting device into 2 rows and 2 columns
## par(mfrow = c(2,2))
##
Expand Down Expand Up @@ -102,7 +102,7 @@
## # log transformation things look ok.


## ----Q9, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q9, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## # to predict bird abundance if AREA == 100
##
## # if you log base 10 transformed the AREA variable
Expand All @@ -112,7 +112,7 @@
## # not log10()


## ----Q10, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE----------------------------------------------------------------------
## ----Q10, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## # need to create a dataframe object with a column of LOGAREA values to predict from.
## # note you need to call the column the same as used in the model
## my.data <- data.frame(LOGAREA = seq(from = min(loyn$LOGAREA),
Expand All @@ -124,7 +124,7 @@
## pred.vals <- predict(loyn.lm, newdata = my.data)


## ----Q11, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE----------------------------------------------------------------------
## ----Q11, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## # plot the lines on the plot. The x values are the new LOGAREA values from the my.data
## # dataframe, the predicted values are from pred.vals
## plot(loyn$LOGAREA, loyn$ABUND, xlab = "Log10 Patch Area",
Expand All @@ -148,7 +148,7 @@
## theme_classic()


## ----Q12, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE----------------------------------------------------------------------
## ----Q12, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## pred.vals.se <- predict(loyn.lm, newdata = my.data, se.fit = TRUE) # note the use of the se.fit argument
##
## # check out the structure of pred.vals.se
Expand Down Expand Up @@ -188,7 +188,7 @@
## theme_classic()


## ----Q13, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE----------------------------------------------------------------------
## ----Q13, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## # back transformed LOGAREA and 95% confidence intervals
##
## # re-plot but this time use the original untransformed AREA variable
Expand Down
26 changes: 13 additions & 13 deletions exercise_solutions/linear_model_2_exercise_solutions.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
## ----Q2, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q2, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## loyn <- read.table("data/loyn.txt", header = TRUE,
## stringsAsFactors = TRUE)
## str(loyn)


## ----Q3, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q3, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## # create factor GRAZE as it was originally coded as an integer
## loyn$FGRAZE <- factor(loyn$GRAZE)
##
## # check this
## class(loyn$FGRAZE)


## ----Q4, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q4, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## boxplot(ABUND ~ FGRAZE, xlab = "Grazing level", ylab = "Bird abundance", data = loyn)
##
## # mean bird abundance for each level of FGRAZE
Expand All @@ -26,11 +26,11 @@
## # levels 2,3 and 4.


## ----Q5, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q5, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## birds.lm <- lm(ABUND ~ FGRAZE, data = loyn)


## ----Q6, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q6, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## anova(birds.lm)
##
## # null hypothesis : There is no difference in the mean bird abundance between the
Expand All @@ -43,7 +43,7 @@
## # of grazing intensity (F_4,62 = 14.98, p < 0.0001)


## ----Q7, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q7, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## summary(birds.lm)
##
## # Here the intercept (baseline) is the mean abundance of birds for FGRAZE level 1.
Expand Down Expand Up @@ -73,7 +73,7 @@
## # significantly different from graze level 5.


## ----Q8, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q8, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## # Set FGRAZE level 2 to be the intercept
##
## loyn$FGRAZE <- relevel(loyn$FGRAZE, ref = "2")
Expand All @@ -97,12 +97,12 @@
## # The intercept is now FGRAZE level 4, we can now compare between levels '4 and 5'


## ----Q9, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## ----Q9, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE------------------------------------------------------------------------
## # The multiple R-squared value is 0.491 and therefore 49.1% of
## # the variation in ABUND is explained by FGRAZE


## ----Q10, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE----------------------------------------------------------------------
## ----Q10, eval=SOLUTIONS, echo=SOLUTIONS, collapse=TRUE-----------------------------------------------------------------------
## # first split the plotting device into 2 rows and 2 columns
## par(mfrow = c(2,2))
##
Expand Down Expand Up @@ -140,7 +140,7 @@
## # generalised least squares. This is not something we will do on this course but will cover in a more advanced statistics course!


## ----Q11a, eval=SOLUTIONS, echo=SOLUTIONS, collapse=FALSE--------------------------------------------------------------------
## ----Q11a, eval=SOLUTIONS, echo=SOLUTIONS, collapse=FALSE---------------------------------------------------------------------
## # Using the gplots package, you may need to install this package first
## # install.packages('gplots')
##
Expand All @@ -150,7 +150,7 @@
## ylab = "bird abundance", data = loyn, connect = FALSE)


## ----Q11b, eval=SOLUTIONS, echo=SOLUTIONS, collapse=FALSE--------------------------------------------------------------------
## ----Q11b, eval=SOLUTIONS, echo=SOLUTIONS, collapse=FALSE---------------------------------------------------------------------
## # Using the effects package, you may need to install this package first
## # install.packages('effects')
##
Expand All @@ -159,7 +159,7 @@
## plot(loyn.effects,"FGRAZE", lty = 0)


## ----Q11c, eval=SOLUTIONS, echo=SOLUTIONS, collapse=FALSE--------------------------------------------------------------------
## ----Q11c, eval=SOLUTIONS, echo=SOLUTIONS, collapse=FALSE---------------------------------------------------------------------
## # and finally using old faithful the predict function and base R graphics
## # with the segments function
##
Expand All @@ -174,7 +174,7 @@
## segments(1:5, pred.vals$fit, 1:5, pred.vals$fit + 1.96 * pred.vals$se.fit)


## ----Q11d, eval=SOLUTIONS, echo=SOLUTIONS, collapse=FALSE--------------------------------------------------------------------
## ----Q11d, eval=SOLUTIONS, echo=SOLUTIONS, collapse=FALSE---------------------------------------------------------------------
## # using old faithful the predict function and base R graphics
## # with the arrows function
##
Expand Down
22 changes: 11 additions & 11 deletions exercise_solutions/linear_model_3_exercise_solutions.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
## ----Q2, eval=SOLUTIONS, echo=SOLUTIONS, results=SOLUTIONS, collapse=TRUE----------------------------------------------------
## ----Q2, eval=SOLUTIONS, echo=SOLUTIONS, results=SOLUTIONS, collapse=TRUE-----------------------------------------------------
## loyn <- read.table("data/loyn.txt", header = TRUE,
## stringsAsFactors = TRUE)
## str(loyn)


## ----Q3, eval=SOLUTIONS, echo=SOLUTIONS, results=SOLUTIONS, collapse=TRUE----------------------------------------------------
## ----Q3, eval=SOLUTIONS, echo=SOLUTIONS, results=SOLUTIONS, collapse=TRUE-----------------------------------------------------
## loyn$LOGAREA <- log10(loyn$AREA)
## # create factor GRAZE as it was originally coded as an integer
## loyn$FGRAZE <- factor(loyn$GRAZE)


## ----Q4, eval=SOLUTIONS, echo=SOLUTIONS, results=SOLUTIONS, collapse=TRUE----------------------------------------------------
## ----Q4, eval=SOLUTIONS, echo=SOLUTIONS, results=SOLUTIONS, collapse=TRUE-----------------------------------------------------
## coplot(ABUND ~ LOGAREA | FGRAZE, data = loyn)
##
## # or
Expand All @@ -27,14 +27,14 @@
## # need to test this with a model.


## ----Q5, eval=SOLUTIONS, echo=SOLUTIONS, results=SOLUTIONS, collapse=TRUE----------------------------------------------------
## ----Q5, eval=SOLUTIONS, echo=SOLUTIONS, results=SOLUTIONS, collapse=TRUE-----------------------------------------------------
## birds_inter1 <- lm(ABUND ~ FGRAZE + LOGAREA + FGRAZE:LOGAREA, data = loyn)
##
## # Or use the 'shortcut' - its is equivalent to the model above
## # Or use the 'shortcut' - it's equivalent to the model above
## # birds.inter.1 <- lm(ABUND ~ FGRAZE * LOGAREA, data = loyn)


## ----Q6, eval=SOLUTIONS, echo=SOLUTIONS, results=SOLUTIONS, collapse=TRUE----------------------------------------------------
## ----Q6, eval=SOLUTIONS, echo=SOLUTIONS, results=SOLUTIONS, collapse=TRUE-----------------------------------------------------
## # first split the plotting device into 2 rows and 2 columns
## par(mfrow = c(2,2))
##
Expand All @@ -58,7 +58,7 @@
## # Leverage plot (all < 0.5).


## ----Q7, eval=SOLUTIONS, echo=SOLUTIONS, results=SOLUTIONS, collapse=TRUE----------------------------------------------------
## ----Q7, eval=SOLUTIONS, echo=SOLUTIONS, results=SOLUTIONS, collapse=TRUE-----------------------------------------------------
## anova(birds_inter1)
##
## # The null hypothesis is that there is no significant interaction between
Expand All @@ -76,7 +76,7 @@
## # dependent on the value of the other variable.


## ----Q8, eval=SOLUTIONS, echo=SOLUTIONS, results=SOLUTIONS, collapse=TRUE----------------------------------------------------
## ----Q8, eval=SOLUTIONS, echo=SOLUTIONS, results=SOLUTIONS, collapse=TRUE-----------------------------------------------------
## summary(birds_inter1)
##
## # (Intercept)
Expand Down Expand Up @@ -134,7 +134,7 @@
## # LOGAREA and FGRAZE as single explanatory variables.


## ----Q9a, eval=SOLUTIONS, echo=SOLUTIONS, collapse=FALSE---------------------------------------------------------------------
## ----Q9a, eval=SOLUTIONS, echo=SOLUTIONS, collapse=FALSE----------------------------------------------------------------------
## par(mfrow= c(1, 1))
## plot(ABUND ~ LOGAREA, data = loyn, col = GRAZE, pch = 16)
## # Note: # colour 1 means black in R
Expand Down Expand Up @@ -209,7 +209,7 @@
## lwd = c(1, 1, 1))


## ----Q9b, eval=SOLUTIONS, echo=SOLUTIONS, collapse=FALSE---------------------------------------------------------------------
## ----Q9b, eval=SOLUTIONS, echo=SOLUTIONS, collapse=FALSE----------------------------------------------------------------------
## # Okay, that was a long-winded way of doing this.
## # If, like me, you prefer more compact code and less risks of errors,
## # you can use a loop, to save repeating the sequence 5 times:
Expand All @@ -231,7 +231,7 @@
## lwd = c(1, 1, 1))


## ----Q9c, eval=SOLUTIONS, echo=SOLUTIONS, collapse=FALSE---------------------------------------------------------------------
## ----Q9c, eval=SOLUTIONS, echo=SOLUTIONS, collapse=FALSE----------------------------------------------------------------------
## # install.packages('ggplot2', dep = TRUE)
## library(ggplot2)
##
Expand Down
Loading

0 comments on commit 396220c

Please sign in to comment.