Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. # David Wells, BIFX 553, 2/16/17 homework
  2.  
  3. # For dat1, I decided that the model works best using untransformed values
  4. # and witout the x1*x2 interaction. Several models with transformed values
  5. # and without the x1*x2 interaction looked good at first but turned out to be
  6. # likely heteroscedastic. The x1*x2 interactions seemded to make the q-q
  7. # plots less linear.
  8.  
  9.  
  10. # Load the packages and data
  11. library(broom)
  12. library(car)
  13. library(tidyverse)
  14. library(splines)
  15.  
  16. load('~/BIFX553/Feb16hw.RData')
  17.  
  18. dat1.lm <- lm(y ~ x1 + x2, data=dat1)
  19.  
  20. # Model Summary
  21. tidy(dat1.lm)
  22.  
  23. # The model isn't improved by included by including x1*x2.
  24. dat1_xinter.lm <- lm(y ~ x1 + x2 + x1*x2, data=dat1)
  25. anova(dat1.lm, dat1_xinter.lm)
  26.  
  27. # The model doesn't show significant autocorrelation.
  28. durbinWatsonTest(dat1.lm)
  29.  
  30. # The model doesn't show significant heteroscedasticity.
  31. ncvTest(dat1.lm)
  32.  
  33. # Q-q plot is linear.
  34. qqp(dat1.lm)
  35.  
  36. # This model shows only one outlier.
  37. outlierTest(dat1.lm)
  38.  
  39. # The variance influence factors are lower than other models'
  40. vif(dat1.lm)
  41.  
  42.  
  43. # For dat2 I took the log10 of the y values and chose the model log10(y) ~ x1.
  44. # I tried several models but they were heteroscedastic and their C+R plots weren't
  45. # linear like the model I chose. My model has only one outlier.
  46.  
  47. dat2 <- mutate(dat2, ly = log10(y))
  48. dat2.lm <- lm(ly ~ x1,data=dat2)
  49. tidy(dat2.lm)
  50. crPlots(dat2.lm)
  51. durbinWatsonTest(dat2.lm)
  52. ncvTest(dat2.lm)
  53. outlierTest(dat2.lm)
  54.  
  55.  
  56. # For dat3 I went with untransformed values but the C+R plot suggests that
  57. # a non-paremetric regression line will fit the data. I used spline to
  58. # plot the line. The model satisfies our other assumptions.
  59.  
  60. dat3.lm <- lm(y~x1,data=dat3)
  61.  
  62. tidy(dat3.lm)
  63. crPlots(dat3.lm)
  64. durbinWatsonTest(dat3.lm)
  65. ncvTest(dat3.lm)
  66. outlierTest(dat3.lm)
  67.  
  68. # Plot the line.
  69.  
  70. dat3_sp <- ggplot(dat3.lm, aes(x = x1, y = y)) +
  71. geom_point() +
  72. geom_smooth(method = "loess")
  73.  
  74. dat3_sp + geom_smooth(method = 'lm', se = FALSE, color = 'orange3',
  75. linetype = 2, formula = y ~ bs(x, knots = 0, degree = 1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement