Created
May 11, 2020 22:56
-
-
Save reuning/bfe7d65808836bfec1cb83a9750e5e84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
N_all <- 1000 | |
out <- numeric(N_all) | |
N <- 50 | |
for(ii in 1:N_all){ | |
set.seed(ii) | |
tmp <- round(rnorm(N, 50, 1.9), 0) | |
tmp <- ((tmp[-1] - tmp[-N])) | |
out[ii] <- sum(zoo::rollapply(tmp, 3, function(x) all(sign(x)==sign(x)[1] ))) | |
} | |
library(ggplot2) | |
p1 <- ggplot(data.frame(x=out), aes(x=x)) + geom_bar() + | |
theme_minimal() + ggtitle("3 Poll Trend") + | |
labs(x="Number of Instances in 50 Polls") + | |
scale_y_continuous("Percentage", | |
labels=scales::label_percent(accuracy=1, | |
scale=.1)) | |
for(ii in 1:N_all){ | |
set.seed(ii) | |
tmp <- round(rnorm(N, 50, 1.9), 0) | |
tmp <- ((tmp[-1] - tmp[-N])) | |
out[ii] <- sum(zoo::rollapply(tmp, 4, function(x) all(sign(x)==sign(x)[1] ))) | |
} | |
p2 <- ggplot(data.frame(x=out), aes(x=x)) + geom_bar() + | |
theme_minimal() + ggtitle("4 Poll Trend") + | |
labs(x="Number of Instances in 50 Polls") + | |
scale_y_continuous("Percentage", | |
labels=scales::label_percent(accuracy=1, | |
scale=.1)) | |
p <- gridExtra::grid.arrange(p1, p2,top="How Often Do Polling Trends Appear?") | |
ggsave(p, file="~/Desktop/trends.png", height=8, width=6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment