Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@b-rodrigues
Created September 16, 2021 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save b-rodrigues/538ae9e45fba1eee596f3de38f83ea1b to your computer and use it in GitHub Desktop.
Save b-rodrigues/538ae9e45fba1eee596f3de38f83ea1b to your computer and use it in GitHub Desktop.
library(broom)
library(purrr)
library(dplyr)
library(ggplot2)
run_spurious_regression <- function(nvars = 6, nobs = 1000){
dataset <- as.data.frame(replicate(nvars, rnorm(nobs)))
tidy(lm(V1 ~ ., data = dataset))
}
many_spurious_regressions <- rerun(1, run_spurious_regression()) %>%
map2(.x = ., .y = seq(1, 1), ~mutate(.x, run = paste0("Run number ", .y))) %>%
bind_rows()
many_spurious_regressions %>%
mutate(is_significant = ifelse(p.value < .05, "Yes", "No")) %>%
ggplot() +
geom_point(aes(y = estimate, x = term, colour = is_significant)) +
labs(title = "Significative results in totally randomly generated data",
subtitle = "6 normally distributed variables of size 1000 were created, and the first was regressed on the 5 others.
The plots show the coefficients from one particularly favorable regression. The colour of the dots show if the coefficients were significant at the 5% level.") +
theme_minimal() +
theme(legend.position = "bottom")
many_spurious_regressions <- rerun(100, run_spurious_regression()) %>%
map2(.x = ., .y = seq(1, 100), ~mutate(.x, run = paste0("Run number ", .y))) %>%
bind_rows()
many_spurious_regressions %>%
mutate(is_significant = ifelse(p.value < .05, "Yes", "No")) %>%
ggplot() +
geom_point(aes(y = estimate, x = term, colour = is_significant)) +
labs(title = "Significative results in totally randomly generated data",
subtitle = "6 normally distributed variables of size 1000 were created, and the first was regressed on the 5 others.
The plots show the coefficients from 100 runs and their colours show if these coefficients were significant at the 5% level.") +
theme_minimal() +
theme(legend.position = "bottom")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment