Skip to content

Instantly share code, notes, and snippets.

@audhiaprilliant
Last active December 19, 2020 02:56
Show Gist options
  • Select an option

  • Save audhiaprilliant/6c605e117628198a8d148f89e6399987 to your computer and use it in GitHub Desktop.

Select an option

Save audhiaprilliant/6c605e117628198a8d148f89e6399987 to your computer and use it in GitHub Desktop.
Simulation of Autocorrelation in Linear Regression Model
# Install package
install.packages('ggplot2')
# Load the package
library(ggplot2)
# Without autocorrelation - intercept
df_InterceptNonAutocorrelation = long %>%
filter(Coefficient == 'interceptNonAutocorrelation')
unique(df_InterceptNonAutocorrelation$Rho)
ggplot(data = df_InterceptNonAutocorrelation)+
geom_boxplot(aes(x = as.factor(Rho),
y = Value),
outlier.shape = NA)+
labs(title = 'Intercept without Autocorrelation',
caption = 'Statistics simulation')+
xlab('Rho')+
ylab('Coefficient')+
theme_minimal()
# Without autocorrelation - slope
df_SlopeNonAutocorrelation = long %>%
filter(Coefficient == 'slopeNonAutocorrelation')
unique(df_SlopeNonAutocorrelation$Rho)
ggplot(data = df_SlopeNonAutocorrelation)+
geom_boxplot(aes(x = as.factor(Rho),
y = Value),
outlier.shape = NA)+
labs(title = 'Slope without Autocorrelation',
caption = 'Statistics simulation')+
xlab('Rho')+
ylab('Coefficient')+
theme_minimal()
# With autocorrelation - intercept
df_InterceptAutocorrelation = long %>%
filter(Coefficient == 'interceptAutocorrelation')
unique(df_InterceptAutocorrelation$Rho)
ggplot(data = df_InterceptAutocorrelation)+
geom_boxplot(aes(x = as.factor(Rho),
y = Value),
outlier.shape = NA)+
labs(title = 'Intercept with Autocorrelation',
caption = 'Statistics simulation')+
xlab('Rho')+
ylab('Coefficient')+
theme_minimal()
# With autocorrelation - slope
df_SlopeNonAutocorrelation = long %>%
filter(Coefficient == 'slopeAutocorrelation')
unique(df_SlopeNonAutocorrelation$Rho)
ggplot(data = df_SlopeNonAutocorrelation)+
geom_boxplot(aes(x = as.factor(Rho),
y = Value),
outlier.shape = NA)+
labs(title = 'Slope with Autocorrelation',
caption = 'Statistics simulation')+
xlab('Rho')+
ylab('Coefficient')+
theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment