Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
## Packages
library(ggplot2)
library(ggthemes)
## Load data
data <- read.csv("canada-accuracy.csv") ## https://open.canada.ca/data/en/dataset/d5f7f93b-f757-42ef-b468-1d3791d97d94
dim(data)
colnames(data)
## Get levels
levels = unique(data[,7])
levels
calibrationList = list()
for(i in c(1:dim(data)[1])){
level = data[i,7]
result = data[i,6]
resultNumeric = ifelse(toString(result) == "Did occur", 1,0)
## print(c(toString(result), resultNumeric))
calibrationList[[toString(level)]] = c(calibrationList[[toString(level)]], resultNumeric)
}
calibrationList
## Analysis
plotdata = list()
plotdata$levels <- unique(data[,7])
plotdata$historical_frequency <- as.numeric(sapply(calibrationList, mean))
data_frame = as.data.frame(plotdata)
## Plots
ggplot(data = data_frame, aes(x=levels, y=historical_frequency)) +
geom_point()+
theme_tufte() +
labs(
title="Calibration of Forecasts in Canadian Strategic Intelligence",
subtitle="n = 1,514",
x="Probability estimate",
y="Historical frequency"
)+ theme(
legend.title = element_blank(),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
legend.box="vertical",
) +
geom_abline(aes(intercept=0, slope=1), color="blue")
height = 5
width = floor(height*(1+sqrt(5))/2)
ggsave("CanadianIntelligence.png", width=width, height=height) ## See plot_as_variable in line 112
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment