Skip to content

Instantly share code, notes, and snippets.

@honake
Created October 16, 2018 13:12
Show Gist options
  • Save honake/95987cb50d4d9cb49e4ec855bc368bd7 to your computer and use it in GitHub Desktop.
Save honake/95987cb50d4d9cb49e4ec855bc368bd7 to your computer and use it in GitHub Desktop.
Spurious Regression in R
library(tidyverse)
library(ggplot2)
set.seed(123)
sample_num <- 1000
sigma <- 1
# Generate Two Unit Root Processes
x <- c(0,cumsum(rnorm(sample_num-1, mean=0, sd=sigma)))
y <- c(0,cumsum(rnorm(sample_num-1, mean=0, sd=sigma)))
# Plot
df %>%
gather(-t, key=var, value=val) %>%
ggplot(aes(x=t, y=val, fill=var)) +
geom_line(aes(col=var), size = 1) +
theme_minimal() +
scale_color_brewer(palette="Paired") +
geom_label(aes(x = 10, y = 30, label = "Here is a line"), fill = "#dddddd") +
labs(title = "SPURIOUS REGRESSION EXAMPLE") +
annotate("text", x = 60, y = 40, hjust=0.08, size=4,
label = "R^2 = 0.25 \n p-value < 2.2e-16", color="#333333", fontface="bold")
# Regression
df <- data.frame(t=1:sample_num, x, y)
summary(lm(y~x,df))
# White Noise
white_noise <- data.frame(t=1:100,x=rnorm(100, mean=0, sd=1))
white_noise %>%
gather(-t, key=var, value=val) %>%
ggplot(aes(x=t, y=val, fill=var)) +
geom_line(aes(col=var), size = 1) +
theme_minimal() +
scale_color_brewer(palette="Paired") +
labs(title = "White Noise (Mean 0, Variance 1)")
# Random Walk
randomWalk <- function(size, d, rho, sigma)
{
return(Reduce(function(x, noise){d+rho*x+noise}, rnorm(size-1, mean=0, sd=sigma), init=0, accumulate=TRUE))
}
rw <- data.frame(t=1:100, x=randomWalk(100, 0, 1, 1))
rw %>%
gather(-t, key=var, value=val) %>%
ggplot(aes(x=t, y=val, fill=var)) +
geom_line(aes(col=var), size = 1) +
theme_minimal() +
scale_color_brewer(palette="Paired") +
labs(title = "Random Walk without Drift")
rwd <- data.frame(t=1:100, x=randomWalk(100, 0.25, 1, 1))
rwd %>%
gather(-t, key=var, value=val) %>%
ggplot(aes(x=t, y=val, fill=var)) +
geom_line(aes(col=var), size = 1) +
theme_minimal() +
scale_color_brewer(palette="Paired") +
labs(title = "Random Walk with Positive Drift")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment