atyre2 / NRES803

Other
0 stars 8 forks source link

check_assumptions() should make sure there are sufficient unique values #14

Open atyre2 opened 3 years ago

atyre2 commented 3 years ago

If there are few unique levels of .fitted, the scale-location and residual vs. fitted plots get whacky with lots of warnings.

library("tidyverse")
library("broom")
library("NRES803")
library("NRES803data")
data(RIKZ)
dim(RIKZ)
RIKZ$Richness <- rowSums(RIKZ[,2:76] > 0)
dim(RIKZ)
richness_week <- ggplot(RIKZ, aes(x = week, y = Richness)) +
  geom_point() +
  geom_smooth(method = "lm")
richness_week
RIKZ_model.2 = lm(Richness ~ week, data = RIKZ)
summary(RIKZ_model.2)
test_RIKZ2 <- augment(RIKZ_model.2, data=RIKZ)
ggplot(test_RIKZ2, aes(x = .fitted, y = .resid)) + 
  geom_point() + 
  geom_smooth()

# this is lifted out of base R qqline()
y <- quantile(test_RIKZ2$.resid, c(0.25, 0.75))
x <- qnorm(c(0.25, 0.75))
slope <- diff(y)/diff(x)
int <- y[1L] - slope * x[1L]

ggplot(test_RIKZ2, aes(sample = .resid)) + 
  stat_qq() + 
  geom_abline(slope = slope, intercept = int)

ggplot(test_RIKZ2, aes(x = .fitted, y = sqrt(abs(.std.resid)))) + 
  geom_point() + 
  geom_smooth() + 
  geom_hline(yintercept = 1)

ggplot(test_RIKZ2, aes(.hat, .std.resid)) +
 geom_vline(size = 2, colour = "white", xintercept = 0) +
  geom_hline(size = 2, colour = "white", yintercept = 0) +
  geom_point(aes(size = .cooksd)) + geom_smooth(se = FALSE)