Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paketnamnsbyte #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rpackage/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Package: ada
Package: adapop
Title: Ada Poll of Polls
Version: 0.4.0
Authors@R:
Expand All @@ -10,7 +10,7 @@ Description: Functionality for Ada Poll of Polls.
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
RoxygenNote: 7.1.2
Depends: R (>= 3.5.0)
Imports:
checkmate,
Expand Down
6 changes: 3 additions & 3 deletions rpackage/R/docs_package.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#' ada: Package to run Stan for poll of polls models.
#' adapop: Package to run Stan for poll of polls models.
#'
#' @description
#' A package to run poll of polls models and Bayeisna election prediction models
#' A package to run poll of polls models and Bayesian election prediction models
#'
#'
#' @importFrom stats rnorm
#' @docType package
#' @name ada
#' @name adapop
NULL
2 changes: 1 addition & 1 deletion rpackage/R/experiment_bash_generation.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ write_uppmax_bash <- function(args, directory, model_config, stan_arguments, sta
}
if(!is.null(model_config)){
model_config <- model_config[!unlist(lapply(model_config, is.na))]
suppressMessages(ada::model_config(args$stan_model, x = model_config))
suppressMessages(adapop::model_config(args$stan_model, x = model_config))
mc <- yaml::as.yaml(model_config)
mc <- gsub(pattern = "\n", replacement = "\n ", mc)
mc <- paste0("hyper_parameters:\n ",mc)
Expand Down
4 changes: 2 additions & 2 deletions rpackage/R/party_color.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Get the party color for a given party abbrivation
#' Get the party color for a given party abbreviation
#'
#' @param x a party abbrivation
#' @param x a party abbreviation
party_color <- function(x){
checkmate::assert_string(x)
# Taken from: https://sv.wikipedia.org/wiki/Mall:Partif%C3%A4rg
Expand Down
2 changes: 1 addition & 1 deletion rpackage/R/poll_of_polls.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ poll_of_polls <- function(y,
}

# SHA is setup both of all
fun_args <- names(formals(ada::poll_of_polls))[-which(names(formals(poll_of_polls)) %in% c("...", "cache_dir"))]
fun_args <- names(formals(adapop::poll_of_polls))[-which(names(formals(poll_of_polls)) %in% c("...", "cache_dir"))]
sha_fun_args <- list(y = y,
model = readLines(smfp),
polls_data = polls_data,
Expand Down
2 changes: 1 addition & 1 deletion rpackage/R/recompile_stanfit.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Recompile a poll_of_polls object
#'
#' @description
#' The function takes a [poll_of_polls] object and recomplie the code an the data
#' The function takes a [poll_of_polls] object and recompile the code an the data
#'
#' @param x a [poll_of_polls] object
#'
Expand Down
4 changes: 2 additions & 2 deletions rpackage/R/reports.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ write_report <- function(..., report, parameters = NULL, output_file = NULL, out
rmd_path <- report
} else {
checkmate::assert_choice(report, choices = supported_reports())
rmd_path <- system.file(file.path("reports", report), package = "ada")
rmd_path <- system.file(file.path("reports", report), package = "adapop")
}

# Store files to use
Expand Down Expand Up @@ -70,7 +70,7 @@ write_report <- function(..., report, parameters = NULL, output_file = NULL, out
#' Supported reports
#' @export
supported_reports <- function(){
dir(system.file(file.path("reports"), package = "ada"))
dir(system.file(file.path("reports"), package = "adapop"))
}


Expand Down
2 changes: 1 addition & 1 deletion rpackage/data-raw/german_polls_elections.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ source("data-raw/german_polls_elections_functions.R")
german_polls <- get_german_polls_data()

# Get all in same format/colnames
german_polls$PublYearMonth <- paste0(lubridate::year(german_polls$Datum), "-", tolower(ada:::month_abbr_en())[lubridate::month(german_polls$Datum)])
german_polls$PublYearMonth <- paste0(lubridate::year(german_polls$Datum), "-", tolower(adapop:::month_abbr_en())[lubridate::month(german_polls$Datum)])
german_polls$Company <- as.factor(german_polls$Institut)
german_polls$Uncertain <- german_polls$`Nichtwähler/Unentschl.`
german_polls$n <- as.integer(german_polls$Befragte)
Expand Down
2 changes: 1 addition & 1 deletion rpackage/data-raw/pd_test.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library(ada)
library(adapop)
data("swe_polls")
pd <- polls_data(y = swe_polls[,3:11]/100,
house = swe_polls$Company,
Expand Down
2 changes: 1 addition & 1 deletion rpackage/data-raw/spanish_polls_elections.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pls[[7]] <- get_wikipedia_spanish_polls_data(wiki_url, xpath = wiki_table_xpath,


spanish_polls <- dplyr::bind_rows(pls)
spanish_polls$PublYearMonth <- paste0(lubridate::year(spanish_polls$to), "-", tolower(ada:::month_abbr_en())[lubridate::month(spanish_polls$to)])
spanish_polls$PublYearMonth <- paste0(lubridate::year(spanish_polls$to), "-", tolower(adapop:::month_abbr_en())[lubridate::month(spanish_polls$to)])
spanish_polls$Company <- as.factor(spanish_polls$house)

spanish_polls$Uncertain <- NA
Expand Down
2 changes: 1 addition & 1 deletion rpackage/data-raw/spanish_polls_elections_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ parse_date <- function(d, info){
checkmate::assert_string(d)
checkmate::assert_string(info)

month_abbr <- ada:::month_abbr_en()
month_abbr <- adapop:::month_abbr_en()

ds <- strsplit(d, split = "–")[[1]]
ds <- stringr::str_trim(ds)
Expand Down
50 changes: 25 additions & 25 deletions rpackage/inst/reports/evaluate_elections.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ output:

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ada)
library(adapop)
paths_to_pop_objects <- "[pop_paths]"
pops <- ada:::rmd_read_pops(paths_to_pop_objects)
pops <- adapop:::rmd_read_pops(paths_to_pop_objects)

parameter_path <- "[parameter_path]"
param_default <- c("full_period" = FALSE,
Expand All @@ -21,16 +21,16 @@ param_default <- c("full_period" = FALSE,
"election_campaigns" = FALSE,
"diagnostics_table" = TRUE,
"model_information" = TRUE)
params <- ada:::rmd_read_params(parameter_path)
params <- ada:::rmd_parse_parameters(params, param_default)
params <- adapop:::rmd_read_params(parameter_path)
params <- adapop:::rmd_parse_parameters(params, param_default)

parties <- ada:::rmd_pops_unique_parties(pops)
parties <- adapop:::rmd_pops_unique_parties(pops)

mi <- ada:::rmd_model_settings_with_unique(pops)
mi <- adapop:::rmd_model_settings_with_unique(pops)
mi <- mi[order(mi$y, mi$polls_time_to, mi$model_setting),]
mi$model <- mi$i

mu <- ada:::rmd_model_settings_unique(pops)
mu <- adapop:::rmd_model_settings_unique(pops)

```

Expand All @@ -54,9 +54,9 @@ if(params$diagnostics_table){

cat("\n## Model calibration \n")

per <- ada:::rmd_last_known_state_percentile(pops)
per <- adapop:::rmd_last_known_state_percentile(pops)
per$ndraws <- NULL
nmi <- ada:::rmd_model_settings_with_unique(pops)
nmi <- adapop:::rmd_model_settings_with_unique(pops)
per <- merge(per, nmi[, c("sha", "model_setting", "polls_time_to")])
model_settings <- sort(unique(per$model_setting))

Expand Down Expand Up @@ -91,8 +91,8 @@ if(params$diagnostics_table){
if(params$diagnostics_table){

cat("\n## Model performance\n")
rmse_res <- ada:::rmd_last_known_state_evaluation_by_model_setting(pops, type = "rmse")
elpd_res <- ada:::rmd_last_known_state_evaluation_by_model_setting(pops, type = "elpd")
rmse_res <- adapop:::rmd_last_known_state_evaluation_by_model_setting(pops, type = "rmse")
elpd_res <- adapop:::rmd_last_known_state_evaluation_by_model_setting(pops, type = "elpd")
res <- cbind(rmse_res[,c("model_setting", "rmse")], elpd_res[,c("elpd", "n")])
print(knitr::kable(res,
digits = 3,
Expand All @@ -105,14 +105,14 @@ if(params$diagnostics_table){
if(params$diagnostics_table){

cat("\n## RMSE by Country\n")
nmi <- ada:::rmd_model_settings_with_unique(pops)
per <- ada:::rmd_last_known_state_evaluation(pops, type = "rmse")
nmi <- adapop:::rmd_model_settings_with_unique(pops)
per <- adapop:::rmd_last_known_state_evaluation(pops, type = "rmse")
per <- merge(per, nmi[, c("sha", "model_setting", "polls_time_to")])
per <- per[order(per$polls_time_to, per$model_setting),]

# Germany
party <- "SPD"
pnm <- make.names(ada:::rmd_get_other_parties_in_known_state(party))
pnm <- make.names(adapop:::rmd_get_other_parties_in_known_state(party))
per_germany <- per[!is.na(per[[party]]), c("polls_time_to", "model_setting", "sha", "ndraws", pnm[pnm%in%colnames(per)])]
if(nrow(per_germany) > 0){
print(knitr::kable(per_germany,
Expand All @@ -123,7 +123,7 @@ if(params$diagnostics_table){

# Sweden
party <- "M"
pnm <- make.names(ada:::rmd_get_other_parties_in_known_state(party))
pnm <- make.names(adapop:::rmd_get_other_parties_in_known_state(party))
per_swe <- per[!is.na(per[[party]]), c("polls_time_to", "model_setting", "sha", "ndraws", pnm[pnm%in%colnames(per)])]
if(nrow(per_swe) > 0){
print(knitr::kable(per_swe,
Expand All @@ -139,14 +139,14 @@ if(params$diagnostics_table){
if(params$diagnostics_table){

cat("\n## ELDP by Country\n")
nmi <- ada:::rmd_model_settings_with_unique(pops)
per <- ada:::rmd_last_known_state_evaluation(pops, type = "elpd")
nmi <- adapop:::rmd_model_settings_with_unique(pops)
per <- adapop:::rmd_last_known_state_evaluation(pops, type = "elpd")
per <- merge(per, nmi[, c("sha", "model_setting", "polls_time_to")])
per <- per[order(per$polls_time_to, per$model_setting),]

# Germany
party <- "SPD"
pnm <- make.names(ada:::rmd_get_other_parties_in_known_state(party))
pnm <- make.names(adapop:::rmd_get_other_parties_in_known_state(party))
per_germany <- per[!is.na(per[[party]]), c("polls_time_to", "model_setting", "sha", "ndraws", pnm[pnm%in%colnames(per)])]
if(nrow(per_germany) > 0){
print(knitr::kable(per_germany,
Expand All @@ -157,7 +157,7 @@ if(params$diagnostics_table){

# Sweden
party <- "M"
pnm <- make.names(ada:::rmd_get_other_parties_in_known_state(party))
pnm <- make.names(adapop:::rmd_get_other_parties_in_known_state(party))
per_swe <- per[!is.na(per[[party]]), c("polls_time_to", "model_setting", "sha", "ndraws", pnm[pnm%in%colnames(per)])]
if(nrow(per_swe) > 0){
print(knitr::kable(per_swe,
Expand All @@ -174,14 +174,14 @@ if(params$diagnostics_table){
if(params$diagnostics_table){

cat("\n## Percentiles by Country\n")
nmi <- ada:::rmd_model_settings_with_unique(pops)
per <- ada:::rmd_last_known_state_percentile(pops)
nmi <- adapop:::rmd_model_settings_with_unique(pops)
per <- adapop:::rmd_last_known_state_percentile(pops)
per <- merge(per, nmi[, c("sha", "model_setting", "polls_time_to")])
per <- per[order(per$polls_time_to, per$model_setting),]

# Germany
party <- "SPD"
pnm <- make.names(ada:::rmd_get_other_parties_in_known_state(party))
pnm <- make.names(adapop:::rmd_get_other_parties_in_known_state(party))
per_germany <- per[!is.na(per[[party]]), c("polls_time_to", "model_setting", "sha", "ndraws", pnm[pnm%in%colnames(per)])]
if(nrow(per_germany) > 0){
print(knitr::kable(per_germany,
Expand All @@ -192,7 +192,7 @@ if(params$diagnostics_table){

# Sweden
party <- "M"
pnm <- make.names(ada:::rmd_get_other_parties_in_known_state(party))
pnm <- make.names(adapop:::rmd_get_other_parties_in_known_state(party))
per_swe <- per[!is.na(per[[party]]), c("polls_time_to", "model_setting", "sha", "ndraws", pnm[pnm%in%colnames(per)])]
if(nrow(per_swe) > 0){
print(knitr::kable(per_swe,
Expand Down Expand Up @@ -224,7 +224,7 @@ if(params$last_election_period){
shift_latent_days = 6L,
from = unname(pop$model_time_range["to"] - 150),
to = unname(pop$model_time_range["to"]),
latent_state_colour = ada:::party_color(party)) +
latent_state_colour = adapop:::party_color(party)) +
geom_known_state(ks, party) +
ggplot2::theme_bw()
tit <- paste0("From ", mi$polls_time_to[j], ": Setting ", mi$model_setting[j], " (model ", mi$i[j], ") ")
Expand All @@ -246,7 +246,7 @@ if(params$last_election_period){
if(params$diagnostics_table){

cat("## Model diagnostics\n")
tab <- ada:::rmd_model_diagnostic_table(pops)
tab <- adapop:::rmd_model_diagnostic_table(pops)
n <- 5
it <- ncol(tab)%/%n; rest <- ncol(tab)%%n
if(rest > 0) it <- it + 1L
Expand Down
22 changes: 11 additions & 11 deletions rpackage/inst/reports/industry_bias.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ output:
knitr::opts_chunk$set(echo = TRUE)


library(ada)
library(adapop)
paths_to_pop_objects <- "[pop_paths]"
pops <- ada:::rmd_read_pops(paths_to_pop_objects)
pops <- adapop:::rmd_read_pops(paths_to_pop_objects)

parameter_path <- "[parameter_path]"
param_default <- c("full_period" = FALSE,
Expand All @@ -25,16 +25,16 @@ param_default <- c("full_period" = FALSE,
"model_information" = TRUE,
"traceplots" = FALSE,
"dev" = "png")
params <- ada:::rmd_read_params(parameter_path)
params <- ada:::rmd_parse_parameters(params, param_default)
params <- adapop:::rmd_read_params(parameter_path)
params <- adapop:::rmd_parse_parameters(params, param_default)

parties <- ada:::rmd_pops_unique_parties(pops)
parties <- adapop:::rmd_pops_unique_parties(pops)

mi <- ada:::rmd_model_settings_with_unique(pops)
mi <- adapop:::rmd_model_settings_with_unique(pops)
mi <- mi[order(mi$y, mi$polls_time_to, mi$model_setting),]
mi$model <- mi$i

mu <- ada:::rmd_model_settings_unique(pops)
mu <- adapop:::rmd_model_settings_unique(pops)

knitr::opts_chunk$set(dev = params$dev)
```
Expand Down Expand Up @@ -63,7 +63,7 @@ if(params$last_election_period){
for(j in 1:nrow(mi)){
cat("\n")
pop <- pops[[mi$i[j]]]
knm <- ada:::rmd_get_kappa_name(pop)
knm <- adapop:::rmd_get_kappa_name(pop)
if(!(party %in% pop$y)) next
no_kappa <- pop$stan_fit@par_dims[[knm]][1]
date_idx <- (length(pop$known_state$date) - no_kappa + 2L):length(pop$known_state$date)
Expand All @@ -87,7 +87,7 @@ if(params$traceplots){
for(j in 1:nrow(mi)){
cat("\n")
pop <- pops[[mi$i[j]]]
knm <- ada:::rmd_get_kappa_name(pop)
knm <- adapop:::rmd_get_kappa_name(pop)
parties <- pop$y
no_kappa <- pop$stan_fit@par_dims[[knm]][1]
param <- paste0(knm, "[", no_kappa, ",", seq_along(pop$y), "]")
Expand Down Expand Up @@ -138,7 +138,7 @@ if(params$traceplots){
for(j in 1:nrow(mi)){
cat("\n")
pop <- pops[[mi$i[j]]]
knm <- ada:::rmd_get_kappa_name(pop)
knm <- adapop:::rmd_get_kappa_name(pop)
parties <- pop$y
no_kappa <- pop$stan_fit@par_dims[[knm]][1]
param <- paste0("sigma_kappa[", seq_along(pop$y), "]")
Expand Down Expand Up @@ -212,7 +212,7 @@ if(params$last_election_period){
if(params$diagnostics_table){

cat("## Model diagnostics\n")
tab <- ada:::rmd_model_diagnostic_table(pops)
tab <- adapop:::rmd_model_diagnostic_table(pops)
n <- 5
it <- ncol(tab)%/%n; rest <- ncol(tab)%%n
if(rest > 0) it <- it + 1L
Expand Down
Loading