Advertisement
dulwichian

Untitled

Apr 30th, 2023
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.54 KB | None | 0 0
  1. #You often want to look at the multiple independent variables effects on one dependent variable.
  2. #for two or more continuous variables you can use a multiple regression with or without interactions
  3. #for example y = b0 +b1*X1+b2*X2+b3*X3+.....
  4. #for two categorical variables you can do a two-way ANOVA.
  5. #we will use a preloaded dataset for the multiple regression example
  6. data(trees) #lets R know to use trees
  7. str(trees) #look at the variables
  8. #plot look at the relationship between height and volume as well as diameter and volume
  9. library(ggplot2)
  10. ggplot(trees, aes(Girth, Volume))+geom_point()
  11. #do the same thing for Height and Volume, but add a plot title
  12. ggplot(trees, aes(Height, Volume))+geom_point()+ggtitle("Relationship between height and volume")
  13.  
  14. #add a line to the points by doing a linear regression and using the y-intercept and slope
  15. ggplot(trees, aes(Girth, Volume))+geom_point()+geom_smooth(method="lm", se=FALSE)
  16. #do the same thing for Height and Volume, but add a plot title
  17. ggplot(trees, aes(Height, Volume))+geom_point()+geom_smooth(method="lm", se=FALSE)+ggtitle("Relationship between height and volume")
  18.  
  19. #lets look to see if we should reject the null that the slope is 0
  20. resgirth=lm(Volume~Girth, data=trees) #linear regression
  21. resgirth #note that the output gives you the Intercept and the slope of the line
  22. summary(resgirth) #look at p-value and adjusted R-squared
  23. #do the same for height and volume
  24. resheight=lm(Volume~Height, data=trees)
  25. resheight
  26. summary(resheight)
  27. #you could get a better predictor of volume if you used both girth and height
  28. #luckily you can do this by just adding to the single variable lm model
  29. resgirthandheight=lm(Volume~Girth+Height, data=trees)
  30. summary(resgirthandheight)
  31. #note that the p-value for the Height is greater than it was in the single variable situation
  32. #this is because the test is essentially asking does height have any effect after taking girth into account and
  33. #it does add something, but not as much as when nothing else has been included previously
  34. #also note that the adjusted R-squared is greater than before (more is explained by the model)
  35. #if there was no interaction between height and girth, we would be done, but there is an interaction
  36. #we can include the interaction term by adding Girth*Height
  37. resgirthandheightinteraction=lm(Volume~Girth+Height+Girth*Height, data=trees) #QUESTION -- Why are we adding Girth*Height
  38. summary(resgirthandheightinteraction)
  39.  
  40. #let's do the same thing for two categorical independent variables.
  41. #I am going to follow examples from this site because there is a very good explanation of
  42. #how to think about interaction plots and it could be a valuable resource for you later.
  43. #https://dzchilds.github.io/stats-for-bio/two-way-anova-in-r.html
  44. setwd("~/Desktop/AS450HO/RTutorialDataAS450/")
  45. festuca <- read.csv("FESTUCA.CSV", header=TRUE)
  46. str(festuca)
  47. View(festuca)
  48. #this gives you the final weight of Festuca plants grown in the presence or absenc of Calluna
  49. #under two different pH conditions. Notice that it is a fully factorial design.
  50. #look at graphs of the data
  51. ggplot(festuca, aes(Calluna, Weight, colour = pH)) +
  52.   geom_boxplot()
  53. festuca_model<-aov(lm(Weight ~ pH + Calluna + pH:Calluna, festuca))
  54. summary(festuca_model)
  55. TukeyHSD(festuca_model, which = 'pH:Calluna')
  56. #interaction plots
  57. with(festuca, interaction.plot(pH, Calluna, Weight, fixed = TRUE)) #QUESTION - What is the point of doing an interaction plot vs a boxplot? The values inbetween don't really mean anything do they...
  58. with(festuca, interaction.plot(Calluna, pH, Weight, fixed = TRUE))
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement