├── .Rbuildignore ├── .github ├── .gitignore └── workflows │ ├── R-CMD-check.yaml │ ├── pkgdown.yaml │ └── test-coverage.yaml ├── .gitignore ├── DESCRIPTION ├── LICENSE ├── LICENSE.md ├── NAMESPACE ├── NEWS.md ├── R ├── bpc.R ├── bpc_btpredict.R ├── bpc_data_transformation.R ├── bpc_exports.R ├── bpc_get_parameters.R ├── bpc_get_probabilities.R ├── bpc_helpers.R ├── bpc_helpers_checks.R ├── bpc_helpers_hpd.R ├── bpc_helpers_indexes.R ├── bpc_object.R ├── bpc_plot.R ├── bpc_rank_of_players.R ├── bpc_s3.R ├── bpcs-package.R ├── data.R ├── utils-pipe.R └── zzz.R ├── README.Rmd ├── README.md ├── _pkgdown.yml ├── bpcs.Rproj ├── codecov.yml ├── cran-comments.md ├── data-raw ├── brasil_soccer_league.R ├── brazil_soccer_league_full.csv ├── optimization_algorithms.R ├── statscomp.csv └── tennis_agresti.R ├── data ├── brasil_soccer_league.rda ├── optimization_algorithms.rda └── tennis_agresti.rda ├── figure ├── unnamed-chunk-151-1.png ├── unnamed-chunk-152-1.png ├── unnamed-chunk-155-1.png ├── unnamed-chunk-156-1.png ├── unnamed-chunk-159-1.png └── unnamed-chunk-163-1.png ├── inst ├── CITATION ├── DockerImages │ └── Dockerfiles ├── logo │ ├── boxing2.png │ ├── create_logo.R │ └── logo.png ├── stan │ ├── bt.stan │ └── license.stan └── testdata │ ├── generate_test_data.R │ ├── test_bt.rda │ ├── test_btU.rda │ ├── test_btUordereffect.rda │ ├── test_bt_subject.rda │ ├── test_btorder.rda │ ├── test_btscores.rda │ ├── test_davidson.rda │ ├── test_davidsonU.rda │ ├── test_davidsonorder.rda │ ├── test_davidsonscores.rda │ └── test_predictors.rda ├── man ├── HPD_higher_from_column.Rd ├── HPD_lower_from_column.Rd ├── bernoulli_rng.Rd ├── bpc.Rd ├── bpcs-package.Rd ├── brasil_soccer_league.Rd ├── btpredict.Rd ├── calculate_p1_win_and_ties.Rd ├── calculate_prob_from_vector.Rd ├── check_convergence_diagnostics.Rd ├── check_if_there_are_na.Rd ├── check_if_there_are_ties.Rd ├── check_numeric_predictor_matrix.Rd ├── check_predictors_df_contains_all_players.Rd ├── check_result_column.Rd ├── check_z_column.Rd ├── compute_scores.Rd ├── compute_ties.Rd ├── create_array_of_par_names.Rd ├── create_bpc_object.Rd ├── create_cluster_index.Rd ├── create_cluster_index_with_existing_lookup_table.Rd ├── create_index.Rd ├── create_index_cluster_lookuptable.Rd ├── create_index_lookuptable.Rd ├── create_index_predictors_with_lookup_table.Rd ├── create_index_with_existing_lookup_table.Rd ├── create_predictor_matrix_with_player_lookup_table.Rd ├── create_predictors_lookup_table.Rd ├── create_subject_predictor_lookuptable.Rd ├── expand_aggregated_data.Rd ├── figures │ ├── README-unnamed-chunk-7-1.png │ ├── README-unnamed-chunk-8-1.png │ └── logo.png ├── fix_ties.Rd ├── get_fit.Rd ├── get_loo.Rd ├── get_model_parameters.Rd ├── get_output_dir.Rd ├── get_parameters.Rd ├── get_parameters_df.Rd ├── get_parameters_plot.Rd ├── get_parameters_posterior.Rd ├── get_parameters_table.Rd ├── get_probabilities.Rd ├── get_probabilities_df.Rd ├── get_probabilities_posterior.Rd ├── get_probabilities_table.Rd ├── get_rank_of_players.Rd ├── get_rank_of_players_df.Rd ├── get_rank_of_players_posterior.Rd ├── get_rank_of_players_table.Rd ├── get_sample_posterior.Rd ├── get_waic.Rd ├── inv_logit.Rd ├── launch_shinystan.Rd ├── load_bpc_model.Rd ├── logit.Rd ├── match_cluster_names_to_cluster_lookup_table.Rd ├── match_player_names_to_lookup_table.Rd ├── optimization_algorithms.Rd ├── pipe.Rd ├── plot.bpc.Rd ├── posterior_predictive.Rd ├── predict.bpc.Rd ├── print.bpc.Rd ├── replace_parameter_index_with_names.Rd ├── sample_stanfit.Rd ├── save_bpc_model.Rd ├── summary.bpc.Rd ├── summary_from_fit.Rd └── tennis_agresti.Rd ├── tests ├── testthat.R └── testthat │ ├── helper-functions.R │ ├── helper-no_error.R │ ├── test-bpc.R │ ├── test-bpc_data_transformation.R │ ├── test-bpc_exports.R │ ├── test-bpc_helpers.R │ ├── test-bpc_helpers_checks.R │ ├── test-bpc_helpers_hpd.R │ ├── test-bpc_helpers_indexes.R │ ├── test-bpc_object.R │ ├── test-stan_bt-subjects.R │ ├── test-stan_bt.R │ ├── test-stan_btU.R │ ├── test-stan_btUordereffect.R │ ├── test-stan_btgeneralized.R │ ├── test-stan_btordereffect.R │ ├── test-stan_davidson.R │ ├── test-stan_davidsonU.R │ ├── test-stan_davidsongeneralized.R │ └── test-stan_davidsonordereffect.R └── vignettes ├── .gitignore ├── Instructions.Rmd ├── a_get_started.Rmd ├── b_ties_and_home_advantage.Rmd ├── bibliography.bib ├── c_bt_random_effects.Rmd └── e_contributing.Rmd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^bpcs\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^data-raw$ 4 | ^README\.Rmd$ 5 | ^codecov\.yml$ 6 | ^_pkgdown\.yml$ 7 | ^docs$ 8 | ^pkgdown$ 9 | ^\.github$ 10 | ^doc$ 11 | ^Meta$ 12 | ^figure$ 13 | ^cran-comments.md$ 14 | ^revdep$ 15 | ^LICENSE\.md$ 16 | ^vignettes/a_get_started\.Rmd$ 17 | ^vignettes/b_ties_and_home_advantage\.Rmd$ 18 | ^vignettes/c_bt_random_effects\.Rmd$ 19 | ^vignettes/d_paper\.Rmd$ 20 | ^vignettes/e_contributing\.Rmd$ 21 | ^tarversions$ 22 | ^.Rhistory$ 23 | ^inst/stan/bt$ 24 | ^inst/stan/bt.exe$ 25 | ^\.bpcs$ 26 | ^tests/testthat/\.bpcs$ 27 | -------------------------------------------------------------------------------- /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check.yaml: -------------------------------------------------------------------------------- 1 | # For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag. 2 | # https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions 3 | name: R-CMD-check 4 | 5 | on: 6 | push: 7 | branches: 8 | - main 9 | - master 10 | pull_request: 11 | branches: 12 | - master 13 | - main 14 | 15 | jobs: 16 | R-CMD-check: 17 | runs-on: ${{ matrix.config.os }} 18 | 19 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 20 | 21 | strategy: 22 | fail-fast: true 23 | matrix: 24 | config: 25 | - {os: windows-latest, r: 'release'} 26 | - {os: macOS-latest, r: 'release'} 27 | - {os: ubuntu-20.04, r: 'release'} 28 | - {os: ubuntu-20.04, r: 'devel'} 29 | 30 | env: 31 | R_REMOTES_NO_ERRORS_FROM_WARNINGS: true 32 | RSPM: ${{ matrix.config.rspm }} 33 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 34 | NOT_CRAN: true 35 | CMDSTAN_VERSION: "2.26.1" 36 | 37 | steps: 38 | - name: cmdstan env vars 39 | run: | 40 | echo "CMDSTAN_PATH=${HOME}/.cmdstanr" >> $GITHUB_ENV 41 | shell: bash 42 | 43 | - uses: n1hility/cancel-previous-runs@v2 44 | with: 45 | token: ${{ secrets.GITHUB_TOKEN }} 46 | if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'" 47 | 48 | - uses: actions/checkout@v2 49 | 50 | - name: Set path for RTools 4.0 51 | if: runner.os == 'Windows' 52 | run: echo "C:/rtools40/usr/bin;C:/rtools40/mingw64/bin" | Out-File -Append -FilePath $env:GITHUB_PATH -Encoding utf8 53 | 54 | - name: Install mingw32-make and check toolchain path 55 | if: runner.os == 'Windows' 56 | run: | 57 | pacman -Syu mingw-w64-x86_64-make --noconfirm 58 | g++ --version 59 | Get-Command g++ | Select-Object -ExpandProperty Definition 60 | mingw32-make --version 61 | Get-Command mingw32-make | Select-Object -ExpandProperty Definition 62 | shell: powershell 63 | 64 | - name: Install MPI 65 | if: runner.os == 'Linux' 66 | run: | 67 | sudo apt-get update 68 | sudo apt-get install -y openmpi-bin 69 | sudo apt-get install libcurl4-openssl-dev r-base 70 | - uses: r-lib/actions/setup-r@v1 71 | with: 72 | r-version: ${{ matrix.config.r }} 73 | 74 | - uses: r-lib/actions/setup-pandoc@v1 75 | 76 | - name: Query dependencies 77 | run: | 78 | install.packages('remotes') 79 | saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2) 80 | writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") 81 | shell: Rscript {0} 82 | 83 | - name: Cache R packages 84 | if: runner.os != 'Windows' 85 | uses: actions/cache@v2 86 | with: 87 | path: ${{ env.R_LIBS_USER }} 88 | key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} 89 | restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- 90 | 91 | - name: Install dependencies 92 | run: | 93 | remotes::install_deps(dependencies = TRUE) 94 | remotes::install_cran("rcmdcheck") 95 | install.packages("posterior", repos = c("https://mc-stan.org/r-packages/", getOption("repos")), type="source") 96 | install.packages("cmdstanr", repos = c("https://mc-stan.org/r-packages/", getOption("repos")), type="source") 97 | shell: Rscript {0} 98 | 99 | - name: Cache cmdstan 100 | uses: actions/cache@v2 101 | with: 102 | path: ${{ env.CMDSTAN_PATH }} 103 | key: ${{ runner.os }}-cmdstan-${{ env.CMDSTAN_VERSION }} 104 | restore-keys: ${{ runner.os }}-cmdstan- 105 | 106 | - name: Install cmdstan 107 | run: | 108 | version <- Sys.getenv("CMDSTAN_VERSION") 109 | url <- sprintf("https://github.com/stan-dev/cmdstan/releases/download/v%s/cmdstan-%s.tar.gz", version, version) 110 | cmdstanr::install_cmdstan(cores = 2, release_url = url) 111 | shell: Rscript {0} 112 | 113 | - name: Check 114 | env: 115 | _R_CHECK_CRAN_INCOMING_: false 116 | run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran", "--ignore-vignettes"), build_args = c("--no-build-vignettes"), error_on = "warning", check_dir = "check") 117 | shell: Rscript {0} 118 | 119 | - name: Show testthat output 120 | if: always() 121 | run: find check -name 'testthat.Rout*' -exec cat '{}' \; || true 122 | shell: bash 123 | 124 | - name: Upload check results 125 | if: failure() 126 | uses: actions/upload-artifact@main 127 | with: 128 | name: ${{ runner.os }}-r${{ matrix.config.r }}-results 129 | path: check 130 | -------------------------------------------------------------------------------- /.github/workflows/pkgdown.yaml: -------------------------------------------------------------------------------- 1 | name: pkgdown 2 | 3 | 'on': 4 | push: 5 | branches: 6 | - main 7 | - master 8 | pull_request: 9 | branches: 10 | - master 11 | - main 12 | 13 | jobs: 14 | R-CMD-check: 15 | runs-on: ${{ matrix.config.os }} 16 | 17 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 18 | 19 | strategy: 20 | fail-fast: true 21 | matrix: 22 | config: 23 | - {os: macOS-latest, r: 'release'} 24 | 25 | env: 26 | R_REMOTES_NO_ERRORS_FROM_WARNINGS: true 27 | RSPM: ${{ matrix.config.rspm }} 28 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 29 | NOT_CRAN: true 30 | CMDSTAN_VERSION: "2.26.1" 31 | 32 | steps: 33 | - name: cmdstan env vars 34 | run: | 35 | echo "CMDSTAN_PATH=${HOME}/.cmdstanr" >> $GITHUB_ENV 36 | shell: bash 37 | 38 | - uses: n1hility/cancel-previous-runs@v2 39 | with: 40 | token: ${{ secrets.GITHUB_TOKEN }} 41 | if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'" 42 | 43 | - uses: actions/checkout@v2 44 | 45 | - name: Install MPI 46 | if: runner.os == 'Linux' 47 | run: | 48 | sudo apt-get update 49 | sudo apt-get install -y openmpi-bin 50 | sudo apt-get install libcurl4-openssl-dev r-base 51 | - uses: r-lib/actions/setup-r@v1 52 | with: 53 | r-version: ${{ matrix.config.r }} 54 | 55 | - uses: r-lib/actions/setup-pandoc@v1 56 | 57 | - name: Query dependencies 58 | run: | 59 | install.packages('remotes') 60 | saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2) 61 | writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") 62 | shell: Rscript {0} 63 | 64 | - name: Cache R packages 65 | if: runner.os != 'Windows' 66 | uses: actions/cache@v2 67 | with: 68 | path: ${{ env.R_LIBS_USER }} 69 | key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} 70 | restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- 71 | 72 | - name: Install dependencies 73 | run: | 74 | remotes::install_deps(dependencies = TRUE) 75 | remotes::install_cran("rcmdcheck") 76 | install.packages("pkgdown") 77 | install.packages("posterior", repos = c("https://mc-stan.org/r-packages/", getOption("repos")), type="source") 78 | install.packages("cmdstanr", repos = c("https://mc-stan.org/r-packages/", getOption("repos")), type="source") 79 | shell: Rscript {0} 80 | 81 | - name: Cache cmdstan 82 | uses: actions/cache@v2 83 | with: 84 | path: ${{ env.CMDSTAN_PATH }} 85 | key: ${{ runner.os }}-cmdstan-${{ env.CMDSTAN_VERSION }} 86 | restore-keys: ${{ runner.os }}-cmdstan- 87 | 88 | - name: Install cmdstan 89 | run: | 90 | version <- Sys.getenv("CMDSTAN_VERSION") 91 | url <- sprintf("https://github.com/stan-dev/cmdstan/releases/download/v%s/cmdstan-%s.tar.gz", version, version) 92 | cmdstanr::install_cmdstan(cores = 2, release_url = url) 93 | shell: Rscript {0} 94 | 95 | - name: Install package 96 | run: R CMD INSTALL . 97 | 98 | - name: Deploy package 99 | run: | 100 | git config --local user.email "actions@github.com" 101 | git config --local user.name "GitHub Actions" 102 | Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE)' 103 | -------------------------------------------------------------------------------- /.github/workflows/test-coverage.yaml: -------------------------------------------------------------------------------- 1 | name: Test coverage 2 | 3 | 4 | 'on': 5 | push: 6 | branches: 7 | - main 8 | - master 9 | pull_request: 10 | branches: 11 | - master 12 | - main 13 | 14 | jobs: 15 | R-CMD-check: 16 | runs-on: ${{ matrix.config.os }} 17 | 18 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 19 | 20 | strategy: 21 | fail-fast: true 22 | matrix: 23 | config: 24 | - {os: macOS-latest, r: 'release'} 25 | 26 | env: 27 | R_REMOTES_NO_ERRORS_FROM_WARNINGS: true 28 | RSPM: ${{ matrix.config.rspm }} 29 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 30 | NOT_CRAN: true 31 | CMDSTAN_VERSION: "2.26.1" 32 | 33 | steps: 34 | - name: cmdstan env vars 35 | run: | 36 | echo "CMDSTAN_PATH=${HOME}/.cmdstanr" >> $GITHUB_ENV 37 | shell: bash 38 | 39 | - uses: n1hility/cancel-previous-runs@v2 40 | with: 41 | token: ${{ secrets.GITHUB_TOKEN }} 42 | if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'" 43 | 44 | - uses: actions/checkout@v2 45 | 46 | - name: Install MPI 47 | if: runner.os == 'Linux' 48 | run: | 49 | sudo apt-get update 50 | sudo apt-get install -y openmpi-bin 51 | sudo apt-get install libcurl4-openssl-dev r-base 52 | - uses: r-lib/actions/setup-r@v1 53 | with: 54 | r-version: ${{ matrix.config.r }} 55 | 56 | - uses: r-lib/actions/setup-pandoc@v1 57 | 58 | - name: Query dependencies 59 | run: | 60 | install.packages('remotes') 61 | saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2) 62 | writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") 63 | shell: Rscript {0} 64 | 65 | - name: Cache R packages 66 | if: runner.os != 'Windows' 67 | uses: actions/cache@v2 68 | with: 69 | path: ${{ env.R_LIBS_USER }} 70 | key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} 71 | restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- 72 | 73 | - name: Install dependencies 74 | run: | 75 | remotes::install_deps(dependencies = TRUE) 76 | remotes::install_cran("rcmdcheck") 77 | install.packages("posterior", repos = c("https://mc-stan.org/r-packages/", getOption("repos")), type="source") 78 | install.packages("cmdstanr", repos = c("https://mc-stan.org/r-packages/", getOption("repos")), type="source") 79 | shell: Rscript {0} 80 | 81 | - name: Cache cmdstan 82 | uses: actions/cache@v2 83 | with: 84 | path: ${{ env.CMDSTAN_PATH }} 85 | key: ${{ runner.os }}-cmdstan-${{ env.CMDSTAN_VERSION }} 86 | restore-keys: ${{ runner.os }}-cmdstan- 87 | 88 | - name: Install cmdstan 89 | run: | 90 | version <- Sys.getenv("CMDSTAN_VERSION") 91 | url <- sprintf("https://github.com/stan-dev/cmdstan/releases/download/v%s/cmdstan-%s.tar.gz", version, version) 92 | cmdstanr::install_cmdstan(cores = 2, release_url = url) 93 | shell: Rscript {0} 94 | 95 | - name: Test coverage 96 | run: covr::codecov(type = "tests") 97 | shell: Rscript {0} 98 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | inst/doc 3 | .Rhistory 4 | .RData 5 | .Rprofile 6 | docs 7 | doc 8 | Meta 9 | /src/*.cc 10 | /src/*.o 11 | /src/*.so 12 | /src/*.hpp 13 | /src/*.gcda 14 | /src/*.gcno 15 | /src/*.temp 16 | /src/*.tmp 17 | /data-raw/data-to-try/* 18 | tarversions 19 | .DS_Store 20 | /inst/stan/bt 21 | .bpcs/* 22 | .bpcs_csv/* 23 | /inst/stan/bt.exe 24 | /tests/testthat/.bpcs/* 25 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: bpcs 2 | Title: Bayesian Paired Comparison Analysis with Stan 3 | Version: 1.2.2 4 | Authors@R: c( 5 | person(given = "David", 6 | family = "Issa Mattos", 7 | role = c("aut", "cre"), 8 | email = "issamattos.david@gmail.com", 9 | comment = c(ORCID = "0000-0002-2501-9926")), 10 | person(given="Erika", 11 | family="Martins Silva Ramos", 12 | role = c("aut"), 13 | email = "erikamsramos@gmail.com", 14 | comment = c(ORCID = "0000-0001-7393-1410")) 15 | ) 16 | Description: Models for the analysis of paired comparison data using Stan. The models include Bayesian versions of the Bradley-Terry model, including random effects, generalized model for predictors, order effect (home advantage) and the variations for the Davidson (1970) model to handle ties. Additionally, we provide a number of functions to facilitate inference and obtaining results with these models. References: Bradley and Terry (1952) ; Davidson (1970) ; Carpenter et al. (2017) . 17 | URL: https://github.com/davidissamattos/bpcs, https://davidissamattos.github.io/bpcs/ 18 | BugReports: https://github.com/davidissamattos/bpcs/issues 19 | License: MIT + file LICENSE 20 | Encoding: UTF-8 21 | LazyData: true 22 | Roxygen: list(markdown = TRUE) 23 | RoxygenNote: 7.1.2 24 | Biarch: true 25 | Depends: 26 | R (>= 4.0.0) 27 | Imports: 28 | dplyr, 29 | tidyr, 30 | stringr, 31 | ggplot2, 32 | gtools, 33 | methods, 34 | cmdstanr (>= 0.4.0), 35 | posterior, 36 | tibble, 37 | tidyselect, 38 | HDInterval, 39 | loo, 40 | magrittr, 41 | stats, 42 | rlang, 43 | knitr, 44 | jtools 45 | SystemRequirements: CmdStan (https://mc-stan.org/users/interfaces/cmdstan) 46 | Remotes: 47 | stan-dev/posterior, 48 | stan-dev/cmdstanr 49 | Suggests: 50 | rmarkdown, 51 | testthat, 52 | covr, 53 | bayesplot, 54 | kableExtra, 55 | shinystan, 56 | rstan (>= 2.20.0) 57 | VignetteBuilder: knitr 58 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2020 2 | COPYRIGHT HOLDER: David Issa Mattos 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2020 David Issa Mattos 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method(plot,bpc) 4 | S3method(predict,bpc) 5 | S3method(print,bpc) 6 | S3method(summary,bpc) 7 | export("%>%") 8 | export(bpc) 9 | export(check_convergence_diagnostics) 10 | export(expand_aggregated_data) 11 | export(get_fit) 12 | export(get_loo) 13 | export(get_output_dir) 14 | export(get_parameters) 15 | export(get_parameters_df) 16 | export(get_parameters_plot) 17 | export(get_parameters_posterior) 18 | export(get_parameters_table) 19 | export(get_probabilities_df) 20 | export(get_probabilities_posterior) 21 | export(get_probabilities_table) 22 | export(get_rank_of_players_df) 23 | export(get_rank_of_players_posterior) 24 | export(get_rank_of_players_table) 25 | export(get_sample_posterior) 26 | export(get_waic) 27 | export(inv_logit) 28 | export(launch_shinystan) 29 | export(load_bpc_model) 30 | export(logit) 31 | export(posterior_predictive) 32 | export(save_bpc_model) 33 | import(methods) 34 | importFrom(ggplot2,ggplot) 35 | importFrom(magrittr,"%>%") 36 | importFrom(rlang,.data) 37 | importFrom(stats,median) 38 | importFrom(stats,quantile) 39 | importFrom(stats,rbinom) 40 | importFrom(stats,var) 41 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # Roadmap 2 | 3 | * TODO: New models being analyzed: 4 | - Add new models for modeling time effects (newer contests have higher impact on the ability than older contests) 5 | - Add model for the Bayesian ELO-type rating system 6 | - Add Glicko and Glicko2 models 7 | - Add models for cumulative comparisons 8 | - TODO: Improve test coverage 9 | * TODO: Get posterior distribution of the parameters without accessing the stanfit object 10 | * TODO: integration with bayesplot to get some of the nice plots that we see there 11 | 12 | # bpcs 1.2.2 13 | 14 | * fixing problems with `solve_ties` when the ties where provided directly (and not as two separate score [issue #8](https://github.com/davidissamattos/bpcs/issues/8) 15 | 16 | # bpcs 1.2.1 17 | 18 | * Ability to add credibility mass and choose HDPI or credible intervals in print and in summary 19 | * New function `check_convergence_diagnostics` for HMC diagnostics. This is printed as default in `print` but not in `summary` 20 | 21 | # bpcs 1.2.0 22 | 23 | * Moving to cmdstanr instead of rstan. 24 | - This will allow us to fix some bugs and tweaks that were not optimal. 25 | - Now we can have faster installations and let cmdstan compile the models. 26 | - There will be some additional time to compile the models for the first time but that is only the first time we use it 27 | - We can now remove the errors from ubsan-clang in CRAN which apparently is a lot of trial and error to solve and not supporting tools from CRAN for identifying that 28 | * The interface of the bpcs will remain (practically) the same 29 | * new function to retrieve the posterior distribution of the parameters `get_parameters_posterior` 30 | * alias to retrieve the summary data frame of the parameters `get_parameters_df` 31 | * Removed dependency on coda 32 | * Now we can specify the probability mass in the parameters and in summary 33 | * rstan and shinystan are now optional 34 | * Fix problems with multiple clusters in the posterior predictive function 35 | 36 | 37 | # bpcs 1.1.0 38 | 39 | * Possibility to add up to 3 intercept random effects (hopefully you will never need more than that) 40 | * Model for subject predictors (see example on the paper) 41 | * Make predictions of submodels with the model_type option (see example on the paper) 42 | * Some small bug fixes 43 | 44 | 45 | # bpcs 1.0.1 46 | 47 | * removed ties_pred from models that do not have ties and from the stan models. 48 | * fixed predict for models with ties, so we return a vector y_pred with 0, 1, 2 and not separate as now 49 | * removed posterior distributions from the get_rank_of_players and get_probabilities. Now we have new functions to obtain the data frame or the posterior distributions separately. The posterior is now returned as matrix 50 | * Probabilities table is now optional in the summary function 51 | * New functions to get the probabilities for specific data `get_probabilities_newdata_df` and `get_probabilities_newdata_posterior` 52 | * Publication ready functions for 53 | - plots: `get_parameters_plot` function and a thin S3 plot wrapper for the same function. Plots are default to APA. 54 | - tables: Functions for publication tables: `get_parameter_table`, `get_probabilities_table` and `get_rank_of_players_table` 55 | * `get_hpdi_parameters` became `get_parameters` and the user specify if credible intervals or hpdi 56 | * Added ties to `expand_aggregated_data`. 57 | * We can get now both credible and HPD intervals in `get_parameters`. n_eff and Rhat are also now possible to add and remove from this df 58 | * Added functions to save and load bpc models 59 | 60 | # bpcs 1.0.0 61 | 62 | * Package is feature complete and reached version 1.0.0 63 | * Test coverage > 80% 64 | * Bayesian computation of different variations of the Bradley-Terry and the Davidson model to handle ties in the contest (including with home advantage, random effects and the generalized model). 65 | * Input accepts a column with the results of the contest or the scores for each player. 66 | * Customize a normal prior distribution for every parameter. 67 | * Compute HDP interval for every parameter with the `get_hpdi_parameters` function 68 | * Compute rank of the players with the `get_rank_of_players` function. 69 | * Compute all the probability combinations for one player beating the other with the `get_probabilities` function. 70 | * Convert aggregated tables of results into long format (one contest per row) with the `expand_aggregated_data.` 71 | * Obtain the posterior distribution for every parameter of the model with the `get_sample_posterior` function. 72 | * Easy predictions using the `predict` function. 73 | * We do not reinforce any table or plotting library! Results are returned as data frames for easier plotting and creating tables 74 | * We reinforce the need to manually specify the model to be used. 75 | * Full documentation available at the package site 76 | * Dependence on Stan >= 2.20 for faster compilation times 77 | * Removing vignettes from building with the package. Now they are available only in the package website 78 | 79 | # bpcs 0.0.0.9000 80 | 81 | * Experimental stage 82 | * Basic functionality 83 | * pkgdown site 84 | * starting testing 85 | * add a few Stan models and starting documentation 86 | 87 | -------------------------------------------------------------------------------- /R/bpc_btpredict.R: -------------------------------------------------------------------------------- 1 | #' This function emulates the generate quantities from stan that is not working in some cases 2 | #' @param standata a list containing the data from stan 3 | #' @param draws a matrix of draws from the parameters of the sampled model 4 | #' @return a matrix with each column represents a posterior prediction of the data and each row a predicted value 5 | btpredict <- function(standata, draws){ 6 | N_total <-standata$N_newdata 7 | n <- nrow(draws) 8 | y_pred<-matrix(data=NA,nrow=n,ncol = N_total) 9 | parameters <- list( 10 | lambda = draws[,startsWith(colnames(draws), 'lambda[')], 11 | gm = draws[,'gm'], 12 | nu = draws[,'nu'], 13 | U1_std = draws[,'U1_std'], 14 | U1 = draws[,startsWith(colnames(draws), 'U1[')], 15 | U2_std = draws[,'U2_std'], 16 | U2 = draws[,startsWith(colnames(draws), 'U2[')], 17 | U3_std = draws[,'U3_std'], 18 | U3 = draws[,startsWith(colnames(draws), 'U3[')], 19 | B = draws[,startsWith(colnames(draws), 'B[')], 20 | S = draws[,startsWith(colnames(draws), 'S[')] 21 | ) 22 | 23 | 24 | for (i in seq(1,N_total)){ 25 | prob <- calculate_p1_win_and_ties(i,standata, parameters, n); 26 | p1_win <- prob$p1_win 27 | p_tie <- prob$p_tie 28 | ties_pred = bernoulli_rng(n, p_tie) 29 | win <- bernoulli_rng(n, p1_win) 30 | y_pred[,i] <- ifelse(ties_pred==1,2,win) 31 | } 32 | 33 | colnames(y_pred) <- paste( 34 | rep('y_pred[',N_total), 35 | seq(1:N_total), 36 | rep(']',N_total), 37 | sep="" 38 | ) 39 | return(y_pred) 40 | } 41 | 42 | #' Calcutate the probability for a single row 43 | #' @param i row number 44 | #' @param standata input standata 45 | #' @param parameters input sampled parameters 46 | #' @param n number of draws 47 | #' @return an array with probability of win1 and probability of tie 48 | calculate_p1_win_and_ties <- function(i,standata,parameters,n){ 49 | z<-NULL 50 | U01 <- NULL 51 | U11 <- NULL 52 | U02 <- NULL 53 | U12 <- NULL 54 | U03 <- NULL 55 | U13 <- NULL 56 | player1_indexes <- standata$player1_indexes 57 | player0_indexes <- standata$player0_indexes 58 | U1_indexes<- standata$U1_indexes 59 | U2_indexes<- standata$U2_indexes 60 | U3_indexes<- standata$U3_indexes 61 | X_subject <- standata$X_subject 62 | S0 <- NULL 63 | S1 <- NULL 64 | N_players <- standata$N_players 65 | 66 | 67 | 68 | if(standata$use_Ordereffect){ 69 | z <- standata$z_player1[i] 70 | }else{ 71 | z <- standata$N_newdata[i] 72 | } 73 | 74 | 75 | if(standata$use_U1){ 76 | index1<- paste('U1[',player1_indexes[i],',',U1_indexes[i],']',sep = "") 77 | index0<- paste('U1[',player0_indexes[i],',',U1_indexes[i],']',sep = "") 78 | U01<-parameters$U1[, index0] 79 | U11<-parameters$U1[, index1] 80 | }else{ 81 | U01<-0 82 | U11<-0 83 | } 84 | 85 | if(standata$use_U2){ 86 | index1<- paste('U2[',player1_indexes[i],',',U2_indexes[i],']',sep = "") 87 | index0<- paste('U2[',player0_indexes[i],',',U2_indexes[i],']',sep = "") 88 | U02<-parameters$U2[, index0] 89 | U12<-parameters$U2[, index1] 90 | }else{ 91 | U02<-0 92 | U12<-0 93 | } 94 | 95 | if(standata$use_U3){ 96 | index1<- paste('U3[',player1_indexes[i],',',U3_indexes[i],']',sep = "") 97 | index0<- paste('U3[',player0_indexes[i],',',U3_indexes[i],']',sep = "") 98 | U03<-parameters$U3[, index0] 99 | U13<-parameters$U3[, index1] 100 | }else{ 101 | U03<-0 102 | U13<-0 103 | } 104 | 105 | 106 | 107 | 108 | # dot product in R %*% 109 | if(standata$use_SubjectPredictors){ 110 | #subsetting 111 | S_p1 <- parameters$S[,startsWith(colnames(parameters$S),paste('S[',player1_indexes[i],sep = ""))] 112 | S_p0 <- parameters$S[,startsWith(colnames(parameters$S),paste('S[',player1_indexes[i],sep = ""))] 113 | S1 <- S_p1 %*% X_subject[i,] 114 | S0 <- S_p0 %*% X_subject[i,] 115 | }else{ 116 | S0<-0 117 | S1<-0 118 | } 119 | 120 | lambda1 <-parameters$lambda[,player1_indexes[i]] + parameters$U1_std*U11 + parameters$U2_std*U12 + parameters$U3_std*U13 +S1 121 | lambda0 <- parameters$lambda[,player0_indexes[i]] + parameters$U1_std*U01 + parameters$U2_std*U02 + parameters$U3_std*U03 +S0 122 | 123 | 124 | geom_term <- standata$use_Davidson*exp(parameters$nu+0.5*(parameters$lambda[,player1_indexes[i]]+parameters$lambda[,player0_indexes[i]])) 125 | p1 <- exp(lambda1) 126 | p0 <- exp(lambda0) 127 | 128 | p1_win <- p1/(p0+p1+geom_term) 129 | p_tie <- geom_term/(p0+p1+geom_term) 130 | 131 | out <- list( 132 | p1_win=p1_win, 133 | p_tie=p_tie) 134 | 135 | return(out) 136 | } 137 | 138 | #' Bernoulli random number distribution 139 | #' @param p probability 140 | #' @param n the size to return 141 | #' @return a random number 1 or 0 142 | #' @importFrom stats rbinom 143 | bernoulli_rng <- function(n,p){ 144 | return(rbinom(n,1,p)) 145 | } 146 | 147 | -------------------------------------------------------------------------------- /R/bpc_data_transformation.R: -------------------------------------------------------------------------------- 1 | #' Expand aggregated data 2 | #' Several datasets for the Bradley-Terry Model aggregate the number of wins for each player in a different column. 3 | #' The models we provide are intended to be used in a long format. A single result for each contest. 4 | #' This function expands datasets that have aggregated data into this long format. 5 | #' @param d a data frame 6 | #' @param player0 string with column name of player0 7 | #' @param player1 string with column name of player1 8 | #' @param wins0 string with column name of the number of wins of player 0 9 | #' @param wins1 string with column name of the number of wins of player 1 10 | #' @param ties string with the column of the ties 11 | #' @param keep an array of strings with the name of columns we want to keep in the new data frame (and repeat in every expanded row) 12 | #' @return a data frame with the expanded dataset. It will have the columns player1, player0, y, the keep columns, and a rowid column (to make each row unique) 13 | #' @export 14 | #' @examples 15 | #' #Creating a simple data frame with only one row to illustrate how the function works 16 | #' df1 <- tibble::tribble(~player0, ~player1, ~wins0, ~wins1,~cluster, 'A','B',4, 3, 'c1') 17 | #' df2 <- expand_aggregated_data(df1,'player0', 'player1', 'wins0', 'wins1', keep=c('cluster')) 18 | #' print(df2) 19 | expand_aggregated_data <- 20 | function(d, player0, player1, wins0, wins1, ties=NULL, keep=NULL) { 21 | #Currently this approach is not the most efficient, but more efficiency hereis also not a priority in the package 22 | d <- as.data.frame(d) 23 | n_d_rows <- nrow(d) 24 | n_d_columns <- ncol(d) 25 | column_names <- c('player0', 'player1', 'y', keep, 'rowid') 26 | n_out_col <- length(column_names) 27 | n_row_out<-NULL 28 | if(is.null(ties)){ 29 | n_row_out <- sum(d[, wins0]) + sum(d[, wins1]) 30 | } else{ 31 | n_row_out <- sum(d[, wins0]) + sum(d[, wins1] + sum(d[,ties])) 32 | } 33 | out <- data.frame(matrix(ncol = n_out_col, nrow = n_row_out)) 34 | colnames(out) <-column_names 35 | j <- 1 36 | for (i in seq_along(1:n_d_rows)) { 37 | current_row <- d[i, ] 38 | n0_rows <- current_row[1, wins0] 39 | n1_rows <- current_row[1, wins1] 40 | if(is.null(ties)){ 41 | n_ties <- 0 42 | } else{ 43 | n_ties <- current_row[1, ties] 44 | } 45 | #First we expand the zero wins 46 | for (k in seq_along(1:n0_rows)) { 47 | out[j, 'player0'] <- current_row[1, player0] 48 | out[j, 'player1'] <- current_row[1, player1] 49 | out[j, 'y'] <- 0 50 | if(!is.null(keep)) 51 | out[j, keep] <- current_row[1, keep] 52 | j <- j + 1 53 | } 54 | #Second we expand the one wins 55 | for (k in seq_along(1:n1_rows)) { 56 | out[j, 'player0'] <- current_row[1, player0] 57 | out[j, 'player1'] <- current_row[1, player1] 58 | out[j, 'y'] <- 1 59 | if(!is.null(keep)) 60 | out[j, keep] <- current_row[1, keep] 61 | j <- j + 1 62 | } 63 | #We only loop here if the ties column is not null 64 | if(!is.null(ties)){ 65 | for (k in seq_along(1:n_ties)) { 66 | out[j, 'player0'] <- current_row[1, player0] 67 | out[j, 'player1'] <- current_row[1, player1] 68 | out[j, 'y'] <- 2 69 | if(!is.null(keep)) 70 | out[j, keep] <- current_row[1, keep] 71 | j <- j + 1 72 | } 73 | } 74 | } 75 | #add the rowid column 76 | out[, 'rowid'] <- seq(1:n_row_out) 77 | out<-tidyr::drop_na(out)#removing columns that are added with empty values 78 | return(out) 79 | } 80 | -------------------------------------------------------------------------------- /R/bpc_helpers.R: -------------------------------------------------------------------------------- 1 | #' Giving a player0 an player1 scores, this functions adds one column to the data frame containing who won (0= player0 1=player1 2=tie) and another if it was a tie. 2 | #' The ties column superseeds the y column. 3 | #' If it was tie the y column does not matter 4 | #' y column: (0= player0 1=player1 2=tie) 5 | #' ties column (0=not tie, 1=tie) 6 | #' @param d dataframe 7 | #' @param player0_score name of the column in data 8 | #' @param player1_score name of the column in data 9 | #' @param solve_ties Method to solve the ties, either randomly allocate, or do nothing, or remove the row from the datasetc('random', 'none', 'remove'). 10 | #' @param win_score decides if who wins is the one that has the highest score or the lowest score 11 | #' @return a dataframe with column 'y' that contains the results of the comparison and a ties column indicating if there was ties 12 | #' @importFrom rlang .data 13 | compute_scores <- function(d, 14 | player0_score, 15 | player1_score, 16 | solve_ties = 'random', 17 | win_score = 'higher') { 18 | d <- as.data.frame(d) 19 | 20 | 21 | d$diff_score <- as.vector(d[, player1_score] - d[, player0_score]) 22 | # If higher score better than lower score for winning 23 | if (win_score == 'higher') 24 | { 25 | player1win <- ifelse(d$diff_score > 0, 1, 26 | ifelse(d$diff_score < 0, 0, 2)) 27 | d$y <- as.vector(player1win) 28 | } 29 | else 30 | #lower 31 | { 32 | player1win <- ifelse(d$diff_score < 0, 1, 33 | ifelse(d$diff_score > 0, 0, 2)) 34 | d$y <- as.vector(player1win) 35 | } 36 | 37 | # How to handle ties in the scores 38 | d <- fix_ties(d, solve_ties = solve_ties) 39 | 40 | 41 | d <- compute_ties(d, 'y') 42 | d <- dplyr::select(d,-.data$diff_score) 43 | return(as.data.frame(d)) 44 | } 45 | 46 | #' Giving a result column we create a new column with ties (0 and 1 if it has) 47 | #' @param d data frame 48 | #' @param result_column column where the result is 49 | #' @return dataframe with a column called ties 50 | compute_ties <- function(d, result_column) { 51 | d <- as.data.frame(d) 52 | if (check_result_column(d[, result_column])) { 53 | d$ties <- ifelse(d[, result_column] == 2, 1, 0) 54 | return(as.data.frame(d)) 55 | } 56 | else 57 | stop('compute_ties: Result column in the wrong format') 58 | 59 | } 60 | 61 | #' fix_ties 62 | #' This functions provides the possible solutions for ties in the data frame 63 | #' @param d a dataframe with the results in the y column 64 | #' @param solve_ties method for solving ties 'random', 'none', or 'remove' 65 | #' 66 | #' @return a data frame with the result column y adjusted 67 | #'@importFrom rlang .data 68 | fix_ties <- function(d, solve_ties = 'random'){ 69 | if (solve_ties == 'none') 70 | { 71 | 72 | } 73 | if (solve_ties == 'random') 74 | { 75 | for (i in 1:nrow(d)) { 76 | if (d$y[i] == 2) 77 | d$y[i] = sample(c(0, 1), replace = T, size = 1) 78 | } 79 | } 80 | if (solve_ties == 'remove') 81 | { 82 | 83 | d$y <- ifelse(d$y == 2, NA,d$y) 84 | d <- tidyr::drop_na(d, .data$y) 85 | } 86 | return(d) 87 | } 88 | 89 | 90 | #' Return all the name of the used parameters in a model from a bpc_object. 91 | #' Here we exclude the log_lik and the lp__ since they are not parameters of the model 92 | #' @param bpc_object a bpc object 93 | #' @return a vector with the name of the parameters 94 | get_model_parameters <- function(bpc_object) { 95 | if (class(bpc_object) != 'bpc') 96 | stop('Error! The object is not of bpc class') 97 | pars <- bpc_object$used_pars 98 | return(pars) 99 | } 100 | 101 | 102 | #' Return a data frame by resampling the posterior from a stanfit 103 | #' Here we select a parameter, retrieve the all the posterior from the stanfit and then we resample this posterior n times 104 | #' @references Stan Development Team (2020). RStan: the R interface to Stan. R package version 2.21.2. http://mc-stan.org/. 105 | #' @param stanfit stanfit object 106 | #' @param par parameter name 107 | #' @param n number of samples 108 | #' @return a dataframe containing the samples of the parameter. Each column is a parameter (in order of the index), each row is a sample 109 | sample_stanfit <- function(stanfit, par, n = 100) { 110 | posterior <- rstan::extract(stanfit) 111 | posterior <- as.data.frame(posterior[[par]]) 112 | #re sampling from the posterior 113 | s <- dplyr::sample_n(posterior, size = n, replace = T) 114 | return(as.data.frame(s)) 115 | } 116 | 117 | 118 | #' Calculate the probability of occurrence of a in a vector x 119 | #' 120 | #' @param x a vector 121 | #' @param a a value in x 122 | #' 123 | #' @return a value of the probability of a in vector x 124 | calculate_prob_from_vector <- function(x, a){ 125 | x_is_a <- x==a 126 | out<-length(x[x_is_a])/length(x) 127 | return(out) 128 | } 129 | 130 | 131 | -------------------------------------------------------------------------------- /R/bpc_helpers_checks.R: -------------------------------------------------------------------------------- 1 | ############# Check functions 2 | 3 | #' Check if a data frame column contains only the values 1 0 and 2. Used to check the format of the results 4 | #' @param d_column a column from a data frame 5 | #' 6 | #' @return TRUE (correct) or FALSE (with problems) 7 | check_result_column <- function(d_column) { 8 | passed <- all(d_column %in% c(2, 0, 1)) 9 | return(passed) 10 | } 11 | 12 | #' Check if a data frame column contains ties 13 | #' @param d_column a column with the values for the ties 14 | #' @return T (there are ties) or F (no ties) 15 | check_if_there_are_ties <- function(d_column) { 16 | ties <- any(d_column %in% c(2)) 17 | return(ties) 18 | } 19 | 20 | 21 | #' Check for NA in the specfic columns and returns T or F is there is at least 1 NA in those columns 22 | #' 23 | #' @param d a data frame 24 | #' @param player0 the name of column for player0 25 | #' @param player1 the name of column for player1 26 | #' @param player0_score the name of column for player0 scores 27 | #' @param player1_score the name of column for player1 scores 28 | #' @param result_column the name of column for results 29 | #' @return TRUE (there are NA) or FALSE (no NA) 30 | check_if_there_are_na <- 31 | function(d, 32 | player0, 33 | player1, 34 | player0_score = NULL, 35 | player1_score = NULL, 36 | result_column = NULL) 37 | { 38 | d <- as.data.frame(d) 39 | na_cols <- 40 | c(player0, 41 | player1, 42 | player0_score, 43 | player1_score, 44 | result_column) 45 | for (col in na_cols) { 46 | check_na <- any(is.na(d[, col])) 47 | if (check_na == T) 48 | return(T) 49 | } 50 | return(F) 51 | } 52 | 53 | 54 | 55 | #' Check if a data frame column contains only the values 1 or 0. For the z column 56 | #' @param d_column a column of a data frame to be tested 57 | #' @return TRUE (correct) or FALSE (with problems) 58 | check_z_column <- function(d_column) { 59 | passed <- all(d_column %in% c(0, 1)) 60 | return(passed) 61 | } 62 | 63 | 64 | #' Check if all values in the predictor matrix are numeric and not NA. Note that TRUE will be cast to 1 and FALSE will be cast to 0 65 | #' @param predictor_matrix a predictor matrix generated by the create_predictor_matrix_with_player_lookup_table function 66 | #' @return TRUE (correct) or FALSE (with problems) 67 | check_numeric_predictor_matrix <- function(predictor_matrix) { 68 | passed <- 69 | all(is.numeric(predictor_matrix)) & all(!is.na(predictor_matrix)) 70 | return(passed) 71 | } 72 | 73 | 74 | #' Check if the predictor df contains all players and only those 75 | #' @param predictor_df the predictor input data frame 76 | #' @param lookup_table a lookup table of the players 77 | #' @return TRUE (correct) or FALSE (with problems) 78 | check_predictors_df_contains_all_players <- 79 | function(predictor_df, lookup_table) { 80 | predictor_df <- as.data.frame(predictor_df) 81 | passed <- TRUE 82 | nplayers <- nrow(lookup_table) 83 | nplayer_predictor_df <- nrow(predictor_df) 84 | if (nplayers != nplayer_predictor_df) 85 | passed <- FALSE 86 | player_column <- predictor_df[, 1] 87 | if (!all(player_column %in% lookup_table$Names)) 88 | passed <- FALSE 89 | return(passed) 90 | } 91 | -------------------------------------------------------------------------------- /R/bpc_helpers_hpd.R: -------------------------------------------------------------------------------- 1 | ############ HPD functions 2 | 3 | 4 | #' Returns the lower value of the HPD interval for a data frame column 5 | #' @references 6 | #' Mike Meredith and John Kruschke (2020). HDInterval: Highest (Posterior) Density Intervals. R package version 0.2.2. https://CRAN.R-project.org/package=HDInterval 7 | #' @param column the data to calculate the HPDI 8 | #' @param credMass Credibility mass for the interval (area contained in the interval) 9 | #' @return the value of the lower HPD interval for that column 10 | #' @importFrom rlang .data 11 | HPD_lower_from_column <- function(column, credMass = 0.95) { 12 | hdi_col <- HDInterval::hdi(column, credMass = credMass) 13 | return(hdi_col[[1]]) 14 | } 15 | 16 | #' Returns the higher value of the HPD interval for a data frame column 17 | #' @references 18 | #' Mike Meredith and John Kruschke (2020). HDInterval: Highest (Posterior) Density Intervals. R package version 0.2.2. https://CRAN.R-project.org/package=HDInterval 19 | #' @param column the data to calculate the HPDI 20 | #' @param credMass Credibility mass for the interval (area contained in the interval) 21 | #' @return the value of the higher HPD interval for that column 22 | #' @importFrom rlang .data 23 | HPD_higher_from_column <- function(column, credMass = 0.95) { 24 | hdi_col <- HDInterval::hdi(column, credMass = credMass) 25 | return(hdi_col[[2]]) 26 | } 27 | 28 | 29 | #' Summary for all parameters from a stanfit object 30 | #' Here we use the coda package 31 | #' @references Martyn Plummer, Nicky Best, Kate Cowles and Karen Vines (2006). CODA: Convergence Diagnosis and Output Analysis for MCMC, R News, vol 6, 7-11 32 | #' @param fit a cmdstanr fit object retrieved from a bpc object 33 | #' @param pars the model parameters 34 | #' @param credMass Credibility mass for the interval 35 | #' @return a data frame with the HPDI calculated from the coda package 36 | #' @importFrom rlang .data 37 | #' @importFrom stats quantile 38 | summary_from_fit <- function(fit, pars, credMass=0.95) 39 | { 40 | 41 | draws <- posterior::as_draws_matrix(fit$draws(pars)) 42 | diagnostics <- posterior::as_draws_matrix(fit$summary(pars)) 43 | 44 | draws_df <- as.data.frame(draws) 45 | diagnostics_df <- as.data.frame(diagnostics) %>% 46 | dplyr::select(.data$rhat, .data$ess_bulk) %>% 47 | dplyr::rename('Rhat'='rhat', 'n_eff'='ess_bulk') 48 | 49 | mean_estimate <- as.data.frame(t(apply(draws_df, 2, mean))) 50 | median_estimate <- as.data.frame(t(apply(draws_df, 2, median))) 51 | hpdi_higher <- as.data.frame(t(apply(draws_df, 2, HPD_higher_from_column, credMass))) 52 | hpdi_lower <- as.data.frame(t(apply(draws_df, 2, HPD_lower_from_column, credMass))) 53 | q_lower <- as.data.frame(t(apply(draws_df, 2, quantile, (1-credMass)/2))) 54 | q_higher <- as.data.frame(t(apply(draws_df, 2, quantile, credMass+(1-credMass)/2))) 55 | 56 | 57 | fit_summary <- t(rbind(mean_estimate, 58 | median_estimate, 59 | hpdi_lower, 60 | hpdi_higher, 61 | q_lower, 62 | q_higher)) 63 | 64 | colnames(fit_summary) <- c("Mean", "Median", "HPD_lower", "HPD_higher", "q_lower", "q_higher") 65 | 66 | 67 | fit_summary <- tibble::rownames_to_column(as.data.frame(fit_summary), "Parameter") 68 | fit_summary <- cbind(fit_summary, diagnostics_df) 69 | 70 | return(as.data.frame(fit_summary)) 71 | } 72 | -------------------------------------------------------------------------------- /R/bpc_object.R: -------------------------------------------------------------------------------- 1 | #' Defines the class bpc and creates the bpc object. 2 | #' To create we need to receive some defined parameters (the arguments from the bpc function), a lookup table and a the stanfit 3 | #' object generated from the rstan sampling procedure 4 | #' 5 | #' @param fit A fit object from cmdstanr 6 | #' @param lookup_table lookup_table dataframe. Two columns one Index the other Names where each each index will match a string in the names 7 | #' @param cluster_lookup_table a lookup table with we have random effects 8 | #' @param model_type the type of the model used to call stan (string) 9 | #' @param standata a list with the data used to call the rstan::sampling procedure 10 | #' @param call_arg a list with the arguments called from the bpc function 11 | #' @param predictors_df the data frame of the predictors for a generalized model 12 | #' @param predictors_lookup_table a lookup table for generalized models 13 | #' @param predictors_matrix a matrix of predictors for generalized models 14 | #' @param subject_predictors_lookup_table a lookup table for the subject predictors models 15 | #' @param subject_predictors_matrix a matrix of predictors for the subject predictors matrix 16 | #' @param used_pars an array with all the parameters set for the model 17 | #' @param output_dir output directory for the csv files 18 | #' @return a bpc object 19 | #' 20 | create_bpc_object <- 21 | function(fit, 22 | lookup_table, 23 | model_type, 24 | standata, 25 | call_arg, 26 | output_dir, 27 | cluster_lookup_table = NULL, 28 | predictors_df = NULL, 29 | predictors_lookup_table = NULL, 30 | predictors_matrix = NULL, 31 | subject_predictors_lookup_table = NULL, 32 | subject_predictors_matrix = NULL, 33 | used_pars='lambda') { 34 | 35 | hpdi <- summary_from_fit(fit, used_pars) 36 | 37 | #Creating the object 38 | obj <- list( 39 | Nplayers = nrow(lookup_table), 40 | fit=fit, 41 | hpdi = hpdi, 42 | lookup_table = lookup_table, 43 | cluster_lookup_table = cluster_lookup_table, 44 | predictors_df = predictors_df, 45 | predictors_lookup_table = predictors_lookup_table, 46 | predictors_matrix = predictors_matrix, 47 | model_type = model_type, 48 | standata = standata, 49 | subject_predictors_lookup_table = subject_predictors_lookup_table, 50 | subject_predictors_matrix = subject_predictors_matrix, 51 | call_arg = call_arg, 52 | used_pars=used_pars, 53 | output_dir = output_dir, 54 | bpcs_version = utils::packageVersion("bpcs") 55 | ) 56 | class(obj) <- "bpc" 57 | return(obj) 58 | } 59 | -------------------------------------------------------------------------------- /R/bpc_plot.R: -------------------------------------------------------------------------------- 1 | #' Return a plot for the parameters estimates based on the HPD interval 2 | #' The returned plot is a caterpillar type of plot 3 | #' @param bpc_object a bpc fitted object 4 | #' @param HPDI use HPD (TRUE) or credible intervals (FALSE) for the plots 5 | #' @param params a vector of string for of the parameters to be plotted 6 | #' @param title the title of the plot 7 | #' @param subtitle optional subtitle for the plot 8 | #' @param xaxis title of the x axis 9 | #' @param yaxis title of the y axis 10 | #' @param rotate_x_labels should the labels be shown horizontally (default, FALSE) or vertically (TRUE) 11 | #' @param APA should the graphic be formatted in APA style (default TRUE) 12 | #' @param keep_par_name keep the parameter name e.g. lambda Graff instead of Graff. Default to T. Only valid for lambda, so we can have better ranks 13 | #' @return a ggplot2 caterpillar plot 14 | #' @importFrom rlang .data 15 | #' @export 16 | #' 17 | #' @examples 18 | #' \donttest{ 19 | #' m<-bpc(data = tennis_agresti, 20 | #' player0 = 'player0', 21 | #' player1 = 'player1', 22 | #' result_column = 'y', 23 | #' model_type = 'bt', 24 | #' solve_ties = 'none') 25 | #' p<-get_parameters_plot(m) 26 | #' p 27 | #' } 28 | get_parameters_plot <- 29 | function(bpc_object, 30 | HPDI = T, 31 | params = c('lambda'), 32 | title = 'Parameter estimates', 33 | subtitle = NULL, 34 | xaxis = 'Parameter', 35 | yaxis = 'Value', 36 | rotate_x_labels = FALSE, 37 | APA=TRUE, 38 | keep_par_name = TRUE) { 39 | df <- get_parameters(bpc_object, 40 | params = params, 41 | HPDI = HPDI, 42 | n_eff = F, 43 | Rhat = F, 44 | keep_par_name = keep_par_name) 45 | 46 | colnames(df) <- c('Parameter', 'Mean', 'Median', 'Lower', 'Higher') 47 | df <- df %>% 48 | dplyr::arrange(.data$Mean) 49 | 50 | param_order <- as.array(df$Parameter) 51 | 52 | out <- ggplot2::ggplot(df, ggplot2::aes(x = factor(.data$Parameter, levels = param_order))) + #to order the variables properly we need to convert to factor and use the levels. It is not enough the order of the dataframe 53 | ggplot2::geom_pointrange(ggplot2::aes( 54 | ymin = .data$Lower, 55 | ymax = .data$Higher, 56 | y = .data$Mean 57 | )) + 58 | ggplot2::labs( 59 | y = yaxis, 60 | x = xaxis, 61 | title = title, 62 | subtitle = subtitle 63 | ) 64 | if(APA) 65 | out <- out + jtools::theme_apa() 66 | if(rotate_x_labels) 67 | out <- out+ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, vjust = 0.5, hjust=1)) #to rotate we need to adjust the anchor position as well 68 | 69 | return(out) 70 | } 71 | -------------------------------------------------------------------------------- /R/bpc_rank_of_players.R: -------------------------------------------------------------------------------- 1 | #' Generate a ranking of the ability based on sampling the posterior distribution of the ranks. 2 | #' This is not exported. Use either the get_rank_of_players_df or the get_rank_of_players_posterior functions 3 | #' @param bpc_object a bpc object 4 | #' @param n Number of times we will sample the posterior 5 | #' @return a list containing the data frame that represents the table and a matrix containing the posterior distribution of the ranks 6 | #' @importFrom rlang .data 7 | #' @importFrom stats var median 8 | get_rank_of_players <- function(bpc_object, n = 1000) { 9 | if (class(bpc_object) != 'bpc') 10 | stop('Error! The object is not of bpc class') 11 | s <- get_sample_posterior(bpc_object, par = 'lambda', n = n, keep_par_name=F) 12 | cols_names <- colnames(s) 13 | s <- dplyr::mutate(s, rown = dplyr::row_number()) 14 | 15 | wide_s <- 16 | tidyr::pivot_longer( 17 | s, 18 | cols = tidyselect::all_of(cols_names), 19 | names_to = "Parameter", 20 | values_to = "value" 21 | ) 22 | rank_df <- wide_s %>% 23 | dplyr::group_by(.data$rown) %>% 24 | dplyr::mutate(Rank = rank(-.data$value, ties.method = 'random')) %>% 25 | dplyr::ungroup() %>% 26 | dplyr::select(-.data$value) %>% 27 | dplyr::group_by(.data$Parameter) %>% 28 | dplyr::summarise( 29 | MedianRank = median(.data$Rank), 30 | MeanRank = mean(.data$Rank), 31 | StdRank = sqrt(var(.data$Rank)), 32 | ) %>% 33 | dplyr::arrange(.data$MedianRank) 34 | 35 | post <- wide_s %>% 36 | dplyr::group_by(.data$rown) %>% 37 | dplyr::mutate(Rank = rank(-.data$value, ties.method = 'random')) %>% 38 | dplyr::ungroup() %>% 39 | dplyr::select(-.data$value) %>% 40 | tidyr::pivot_wider(names_from = .data$Parameter, values_from=.data$Rank) %>% 41 | dplyr::select(-.data$rown) %>% 42 | as.matrix() 43 | 44 | out <- list(Table = as.data.frame(rank_df), 45 | Posterior = post) 46 | 47 | return(out) 48 | } 49 | 50 | 51 | #' Generate a ranking of the ability based on sampling the posterior distribution of the ranks. 52 | #' 53 | #' To print this object you should remove the last column PosteriorRank since it contain the whole posterior distribution for each case 54 | #' @param bpc_object a bpc object 55 | #' @param n Number of times we will sample the posterior 56 | #' @return a data frame. This data frame contains the median of the rank, the mean, the standard deviation of the rank 57 | #' @export 58 | #' @importFrom rlang .data 59 | #' @importFrom stats var median 60 | #' @examples 61 | #' \donttest{ 62 | #' m<-bpc(data = tennis_agresti, 63 | #' player0 = 'player0', 64 | #' player1 = 'player1', 65 | #' result_column = 'y', 66 | #' model_type = 'bt', 67 | #' solve_ties = 'none') 68 | #' rank_m<-get_rank_of_players_df(m,n=100) 69 | #' print(rank_m) 70 | #' } 71 | get_rank_of_players_df <- function(bpc_object, n = 1000) { 72 | if (class(bpc_object) != 'bpc') 73 | stop('Error! The object is not of bpc class') 74 | rank_m <- get_rank_of_players(bpc_object,n=n) 75 | rank_df<- rank_m$Table 76 | return(rank_df) 77 | } 78 | 79 | 80 | #' Generate a ranking of the ability based on sampling the posterior distribution of the ranks. 81 | #' 82 | #' To print this object you should remove the last column PosteriorRank since it contain the whole posterior distribution for each case 83 | #' @param bpc_object a bpc object 84 | #' @param n Number of times we will sample the posterior 85 | #' @return a matrix containing the posterior distribution of the ranks 86 | #' @export 87 | #' @importFrom rlang .data 88 | #' @importFrom stats var median 89 | #' @examples 90 | #' \donttest{ 91 | #' m<-bpc(data = tennis_agresti, 92 | #' player0 = 'player0', 93 | #' player1 = 'player1', 94 | #' result_column = 'y', 95 | #' model_type = 'bt', 96 | #' solve_ties = 'none') 97 | #' rank_m<-get_rank_of_players_posterior(m,n=100) 98 | #' print(rank_m) 99 | #' } 100 | get_rank_of_players_posterior <- function(bpc_object, n = 1000) { 101 | if (class(bpc_object) != 'bpc') 102 | stop('Error! The object is not of bpc class') 103 | rank_m<-get_rank_of_players(bpc_object,n=n) 104 | rank_post<- rank_m$Posterior 105 | return(rank_post) 106 | } 107 | 108 | 109 | #' Publication-ready table for the rank table 110 | #' 111 | #' @param bpc_object a bpc object 112 | #' @param format A character string. same formats utilized in the knitr::kable function 113 | #' * 'latex': output in latex format 114 | #' * 'simple': appropriated for the console 115 | #' * 'pipe': Pandoc's pipe tables 116 | #' * 'html': for html formats 117 | #' * 'rst' 118 | #' * 'simple' appropriated for the console 119 | #' @param digits number of digits in the table 120 | #' @param caption a string containing the caption of the table 121 | #' @param n number of times we are sampling the posterior 122 | #' @return a formatted table 123 | #' @export 124 | #' 125 | #' @examples 126 | #' \donttest{ 127 | #' m<-bpc(data = tennis_agresti, 128 | #' player0 = 'player0', 129 | #' player1 = 'player1', 130 | #' result_column = 'y', 131 | #' model_type = 'bt', 132 | #' solve_ties = 'none') 133 | #' t<-get_rank_of_players_table(m) 134 | #' print(t) 135 | #' } 136 | get_rank_of_players_table <- 137 | function(bpc_object, 138 | format = 'latex', 139 | digits = 3, 140 | caption = 'Estimated posterior ranks', 141 | n = 1000) { 142 | if (class(bpc_object) != 'bpc') 143 | stop('Error! The object is not of bpc class') 144 | t <- get_rank_of_players_df(bpc_object , n = n) 145 | out <- 146 | knitr::kable(t, 147 | format = format, 148 | digits = digits, 149 | caption = caption, 150 | booktabs = T) 151 | return(out) 152 | } 153 | 154 | 155 | -------------------------------------------------------------------------------- /R/bpcs-package.R: -------------------------------------------------------------------------------- 1 | #' bpcs - A package for Bayesian Paired Comparison analysis with Stan 2 | #' 3 | #' @description bpcs - A package for Bayesian Paired Comparison analysis with Stan 4 | #' 5 | #' @docType package 6 | #' @name bpcs-package 7 | #' @import methods 8 | #' @importFrom ggplot2 ggplot 9 | #' @importFrom magrittr "%>%" 10 | #' 11 | #' @references 12 | #' Stan Development Team (2020). RStan: the R interface to Stan. R package version 2.21.2. https://mc-stan.org 13 | #' 14 | #' 15 | #' 16 | 17 | NULL 18 | -------------------------------------------------------------------------------- /R/data.R: -------------------------------------------------------------------------------- 1 | #' This is the expansion of the tennis data from Agresti (2003) p.449 2 | #' This data refers to matches for several women tennis players during 1989 and 1990 3 | #' @name tennis_agresti 4 | #' @docType data 5 | #' @format This is the expansion of the data where each row contains 1 match only 6 | #' * player0: name of player0 7 | #' * player1: name of player1 8 | #' * y: corresponds to the result of the match: 0 if player0 won, 1 if player1 won. 9 | #' * id: is a column to make each row unique in the data. It does not have any particular interpretation 10 | #' @source Agresti, Alan. Categorical data analysis. Vol. 482. John Wiley & Sons, 2003. 11 | #' @keywords data 12 | "tennis_agresti" 13 | 14 | 15 | #' This is a dataset with the results matches fromo the first league of the Brazilian soccer championship from 2017-2019. 16 | #' It was reduced and translatedfrom the adaduque/Brasileirao_Dataset repository 17 | #' @name brasil_soccer_league 18 | #' @docType data 19 | #' @format Data frame that contains 1140 matches and 9 Columns from the Brazilian soccer championship 20 | #' * Time: time of the day in 24h format 21 | #' * DayWeek: day of the week 22 | #' * Date: date YY-MM-DD 23 | #' * HomeTeam: name of the team playing home 24 | #' * VisitorTeam: name of the team playing visitor 25 | #' * Round: Round number of the championship 26 | #' * Stadium: Name of the stadium where the game was played 27 | #' * ScoreHomeTeam: number of goals for the home team 28 | #' * ScoreVisitorTeam: number of goals for the visitor 29 | #' @source \url{https://github.com/adaoduque/Brasileirao_Dataset} 30 | #' @keywords data 31 | "brasil_soccer_league" 32 | 33 | 34 | #' Dataset containing an example of the performance of different optimization algorithms against different benchmark functions. 35 | #' This is a reduced version of the dataset presented at the paper: "Statistical Models for the Analysis of Optimization Algorithms with Benchmark Functions.". 36 | #' For details on how the data was collected we refer to the paper. 37 | #' @name optimization_algorithms 38 | #' @docType data 39 | #' @format This is the expansion of the data where each row contains 1 match only 40 | #' * Algorithm: name of algorithm 41 | #' * Benchmark: name of the benchmark problem 42 | #' * TrueRewardDifference: Difference between the minimum function value obtained by the algorithm and the known global minimum 43 | #' * Ndimensions: Number of dimensions of the benchmark problem 44 | #' * MaxFevalPerDimensions: Maximum allowed budget for the algorithm per dimensions of the benchmark problem 45 | #' * simNumber: id of the simulation. Indicates the repeated measures of each algorithm in each benchmark 46 | #' @source Mattos, David Issa, Jan Bosch, and Helena Holmstrom Olsson. Statistical Models for the Analysis of Optimization Algorithms with Benchmark Functions. arXiv preprint arXiv:2010.03783 (2020). 47 | #' @keywords data 48 | "optimization_algorithms" 49 | -------------------------------------------------------------------------------- /R/utils-pipe.R: -------------------------------------------------------------------------------- 1 | #' Pipe operator 2 | #' 3 | #' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. 4 | #' 5 | #' @name %>% 6 | #' @rdname pipe 7 | #' @keywords internal 8 | #' @export 9 | #' @importFrom magrittr %>% 10 | #' @usage lhs \%>\% rhs 11 | NULL 12 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | .onAttach <- function(...) { 2 | ver <- utils::packageVersion("bpcs") 3 | mess <- 4 | paste("This is the version", ver, "of the bpcs package.", 5 | "\nThe bpcs package requires an installation of cmdstan and an appropriated toolchain.", 6 | sep = " " 7 | ) 8 | 9 | 10 | packageStartupMessage(mess) 11 | # 12 | # "\nFor help configuring the toolchain visit:", 13 | # "\nhttps://mc-stan.org/cmdstanr/articles/cmdstanr.html", 14 | # "\nTo install cmdstan run: cmdstanr::install_cmdstan()\n", 15 | # 'cmdstan version: ', cmdstanr::cmdstan_version()) 16 | # cmdstanr::check_cmdstan_toolchain() 17 | } 18 | -------------------------------------------------------------------------------- /_pkgdown.yml: -------------------------------------------------------------------------------- 1 | home: 2 | title: bpcs - A package for Bayesian Paired Comparison analysis with Stan 3 | description: The bpcs package provides pre compiled Stan models for the analysis of paired comparison data. The models include Bayesian versions of the Bradley-Terry model, including random effects (1 level), generalized model for predictors, order effect (home advantage) and the same variations for the Davidson (1970) model to handle ties. Additionally, we provide a number of functions to facilitate inference and obtaining results with these models. 4 | 5 | template: 6 | params: 7 | bootswatch: flatly 8 | 9 | navbar: 10 | title: "bpcs" 11 | left: 12 | - icon: fa-home fa-lg 13 | href: index.html 14 | - text: "Functions" 15 | href: reference/index.html 16 | - text: "Articles" 17 | menu: 18 | - text: "Getting started" 19 | href: articles/a_get_started.html 20 | - text: "Ties and me advantage" 21 | href: articles/b_ties_and_home_advantage 22 | - text: "Random effects" 23 | href: articles/c_bt_random_effects 24 | - text: "Paper: Bayesian Paired-Comparison with the bpcs package" 25 | href: https://arxiv.org/pdf/2101.11227.pdf 26 | - text: "Online appendix for the paper" 27 | href: https://davidissamattos.github.io/bpcs-online-appendix/ 28 | - text: "Contributing guide" 29 | href: articles/e_contributing.html 30 | - text: "News" 31 | href: news/index.html 32 | reference: 33 | - title: "Basic usage functions" 34 | desc: "Basic functions that are used in the R console for model fitting and simple investigations." 35 | - contents: 36 | - bpc 37 | - summary.bpc 38 | - print.bpc 39 | - predict.bpc 40 | - plot.bpc 41 | - get_probabilities_df 42 | - get_rank_of_players_df 43 | - title: "Model checking functions" 44 | desc: "These are a thin wrapper over other packages functions to facilitate the investigation of the convergence of the MCMC." 45 | - contents: 46 | - check_convergence_diagnostics 47 | - launch_shinystan 48 | - get_waic 49 | - get_loo 50 | - get_stanfit 51 | - get_stanfit_summary 52 | - title: "Publication-ready functions" 53 | desc: "Functions used to generate publication-ready plots and tables." 54 | - contents: 55 | - get_rank_of_players_table 56 | - get_parameters_table 57 | - get_probabilities_table 58 | - get_parameters_plot 59 | - title: "Posterior distribution functions" 60 | desc: "Functions used to investigate the posterior distribution." 61 | - contents: 62 | - get_sample_posterior 63 | - get_rank_of_players_posterior 64 | - get_probabilities_posterior 65 | - title: "Data Transformation" 66 | desc: "Functions that provide that transformations." 67 | - contents: 68 | - expand_aggregated_data 69 | - title: "Datasets" 70 | desc: "Description of the built-in datasets." 71 | - contents: 72 | - tennis_agresti 73 | - brasil_soccer_league 74 | - optimization_algorithms 75 | -------------------------------------------------------------------------------- /bpcs.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: XeLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageCheckArgs: --as-cran 22 | PackageRoxygenize: rd,collate,namespace 23 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | comment: false 2 | 3 | coverage: 4 | status: 5 | project: 6 | default: 7 | target: auto 8 | threshold: 1% 9 | informational: true 10 | patch: 11 | default: 12 | target: auto 13 | threshold: 1% 14 | informational: true 15 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | # bpcs 1.1.0 2 | 3 | ## Test environments 4 | 5 | Manual checks: 6 | - local: x86_64-apple-darwin17.0 (64-bit) R version 4.0.3 (2020-10-10) ----> OK 7 | 8 | Github actions: 9 | - os: windows-latest, r: 'release' ----> OK 10 | - os: macOS-latest, r: 'release' ----> OK 11 | - os: ubuntu-20.04, r: 'release' ----> OK 12 | - os: ubuntu-20.04, r: 'devel' ----> OK 13 | 14 | ## R CMD check --as-cran results 15 | 16 | There were 0 errors There were 0 warnings There was 1 note - checking for GNU extensions in Makefiles ... NOTE GNU make is a SystemRequirements. 17 | 18 | ## revdep 19 | 20 | There are no reverse dependencies 21 | 22 | ------------------------------------------------------------------------ 23 | 24 | # bpcs 1.0.0 25 | 26 | This is the first submission of this package 27 | 28 | ## Test environments 29 | 30 | Manual checks: 31 | - local: x86_64-apple-darwin17.0 (64-bit) R version 4.0.3 (2020-10-10) ----> OK 32 | - win-builder-release ----> OK 33 | - win-builder-devel ----> OK 34 | 35 | Github actions: 36 | - os: windows-latest, r: 'release' ----> OK 37 | - os: macOS-latest, r: 'release' ----> OK - 38 | os: ubuntu-20.04, r: 'release' ----> OK 39 | - os: ubuntu-20.04, r: 'devel' ----> OK 40 | 41 | ## R CMD check --as-cran results 42 | 43 | There were 0 errors There were 0 warnings There were 2 notes - checking for GNU extensions in Makefiles ... NOTE GNU make is a SystemRequirements. - checking CRAN incoming feasibility ... NOTE New submission 44 | 45 | ## revdep 46 | 47 | There are no reverse dependencies 48 | -------------------------------------------------------------------------------- /data-raw/brasil_soccer_league.R: -------------------------------------------------------------------------------- 1 | ## code to prepare `brasil_soccer_league` dataset goes here 2 | d<-readr::read_csv('./data-raw/brazil_soccer_league_full.csv') 3 | brasil_soccer_league <-d %>% 4 | dplyr::rename('Time'='Horário', 5 | 'WeekDay'='Dia', 6 | 'Date'='Data', 7 | 'HomeTeam'='Clube 1', 8 | 'VisitorTeam'='Clube 2', 9 | 'Stadium'='Arena', 10 | 'ScoreHomeTeam'='Clube 1 Gols', 11 | 'ScoreVisitorTeam'='Clube 2 Gols', 12 | 'Round'='Rodada') %>% 13 | dplyr::select(-Vencedor, -'Clube 1 Estado', -'Clube 2 Estado', -'Estado Clube Vencedor') %>% 14 | dplyr::filter(Date >= as.Date("2017-01-01") & Date <= as.Date("2019-12-31")) %>% 15 | dplyr::mutate(Round=case_when(Round=="1ª Rodada"~ 1, 16 | Round=="2ª Rodada"~ 2, 17 | Round=="3ª Rodada"~ 3, 18 | Round=="4ª Rodada"~ 4, 19 | Round=="5ª Rodada"~ 5, 20 | Round=="6ª Rodada"~ 6, 21 | Round=="7ª Rodada"~ 7, 22 | Round=="8ª Rodada"~ 8, 23 | Round=="9ª Rodada"~ 9, 24 | Round=="10ª Rodada"~ 10, 25 | Round=="11ª Rodada"~ 11, 26 | Round=="12ª Rodada"~ 12, 27 | Round=="13ª Rodada"~ 13, 28 | Round=="14ª Rodada"~ 14, 29 | Round=="15ª Rodada"~ 15, 30 | Round=="16ª Rodada"~ 16, 31 | Round=="17ª Rodada"~ 17, 32 | Round=="18ª Rodada"~ 18, 33 | Round=="19ª Rodada"~ 19, 34 | Round=="20ª Rodada"~ 20, 35 | Round=="21ª Rodada"~ 21, 36 | Round=="22ª Rodada"~ 22, 37 | Round=="23ª Rodada"~ 23, 38 | Round=="24ª Rodada"~ 24, 39 | Round=="25ª Rodada"~ 25, 40 | Round=="26ª Rodada"~ 26, 41 | Round=="27ª Rodada"~ 27, 42 | Round=="28ª Rodada"~ 28, 43 | Round=="29ª Rodada"~ 29, 44 | Round=="30ª Rodada"~ 30, 45 | Round=="31ª Rodada"~ 31, 46 | Round=="32ª Rodada"~ 32, 47 | Round=="33ª Rodada"~ 33, 48 | Round=="34ª Rodada"~ 34, 49 | Round=="35ª Rodada"~ 35, 50 | Round=="36ª Rodada"~ 36, 51 | Round=="37ª Rodada"~ 37, 52 | Round=="38ª Rodada"~ 38)) %>% 53 | dplyr::mutate(WeekDay=case_when(WeekDay=="Segunda-feira"~ 'Monday', 54 | WeekDay=="Terça-feira"~ 'Tuesday', 55 | WeekDay=="Quarta-feira"~ 'Wednesday', 56 | WeekDay=="Quinta-feira"~ 'Thursday', 57 | WeekDay=="Sexta-feira"~ 'Friday', 58 | WeekDay=="Sábado"~ 'Saturday', 59 | WeekDay=="Domingo"~ 'Sunday')) 60 | usethis::use_data(brasil_soccer_league, overwrite = TRUE) 61 | -------------------------------------------------------------------------------- /data-raw/optimization_algorithms.R: -------------------------------------------------------------------------------- 1 | ## code to prepare `optimization_algorithms` dataset goes here 2 | optimization_algorithms<-readr::read_csv('data-raw/statscomp.csv') %>% 3 | dplyr::select(Algorithm, Benchmark=CostFunction, TrueRewardDifference, Ndimensions, OptimizationSuccessful, MaxFevalPerDimensions, simNumber,SD) %>% 4 | dplyr::filter(MaxFevalPerDimensions==100000) %>% 5 | dplyr::filter(OptimizationSuccessful==TRUE) %>% 6 | dplyr::filter(SD==0) %>% 7 | dplyr::filter(Algorithm!="RandomSearch2" & Algorithm!="CuckooSearch") %>% 8 | dplyr::select(-OptimizationSuccessful, -SD) 9 | 10 | usethis::use_data(optimization_algorithms, overwrite = TRUE) 11 | -------------------------------------------------------------------------------- /data-raw/tennis_agresti.R: -------------------------------------------------------------------------------- 1 | ## code to prepare `tennis_agresti` dataset goes here 2 | tennis_agresti <- tibble::tribble(~player0, ~player1, ~y, ~id, 3 | 'Seles', 'Graf', 0, 1, 4 | 'Seles', 'Graf', 0, 2, 5 | 'Seles', 'Graf', 1, 3, 6 | 'Seles', 'Graf', 1, 4, 7 | 'Seles', 'Graf', 1, 5, 8 | 'Seles', 'Sabatini', 0, 6, 9 | 'Seles', 'Navratilova', 0, 7, 10 | 'Seles', 'Navratilova', 0, 8, 11 | 'Seles', 'Navratilova', 0, 9, 12 | 'Seles', 'Navratilova', 1, 10, 13 | 'Seles', 'Navratilova', 1, 11, 14 | 'Seles', 'Navratilova', 1, 12, 15 | 'Seles', 'Sanchez', 0, 13, 16 | 'Seles', 'Sanchez', 0, 14, 17 | 'Graf', 'Sabatini', 0, 15, 18 | 'Graf', 'Sabatini', 0, 16, 19 | 'Graf', 'Sabatini', 0, 17, 20 | 'Graf', 'Sabatini', 0, 18, 21 | 'Graf', 'Sabatini', 0, 19, 22 | 'Graf', 'Sabatini', 0, 20, 23 | 'Graf', 'Sabatini', 1, 21, 24 | 'Graf', 'Sabatini', 1, 22, 25 | 'Graf', 'Sabatini', 1, 23, 26 | 'Graf', 'Navratilova', 0, 24, 27 | 'Graf', 'Navratilova', 0, 25, 28 | 'Graf', 'Navratilova', 0, 26, 29 | 'Graf', 'Sanchez', 0, 27, 30 | 'Graf', 'Sanchez', 0, 28, 31 | 'Graf', 'Sanchez', 0, 29, 32 | 'Graf', 'Sanchez', 0, 30, 33 | 'Graf', 'Sanchez', 0, 31, 34 | 'Graf', 'Sanchez', 0, 32, 35 | 'Graf', 'Sanchez', 0, 33, 36 | 'Graf', 'Sanchez', 1, 34, 37 | 'Sabatini', 'Navratilova', 0, 35, 38 | 'Sabatini', 'Navratilova', 1, 36, 39 | 'Sabatini', 'Navratilova', 1, 37, 40 | 'Sabatini', 'Sanchez', 0, 38, 41 | 'Sabatini', 'Sanchez', 0, 39, 42 | 'Sabatini', 'Sanchez', 0, 40, 43 | 'Sabatini', 'Sanchez', 1, 41, 44 | 'Sabatini', 'Sanchez', 1, 42, 45 | 'Navratilova', 'Sanchez',0, 43, 46 | 'Navratilova', 'Sanchez',0, 44, 47 | 'Navratilova', 'Sanchez',0, 45, 48 | 'Navratilova', 'Sanchez',1, 46 49 | ) 50 | tennis_agresti <- as.data.frame(tennis_agresti) 51 | 52 | usethis::use_data(tennis_agresti, overwrite = TRUE) 53 | -------------------------------------------------------------------------------- /data/brasil_soccer_league.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/data/brasil_soccer_league.rda -------------------------------------------------------------------------------- /data/optimization_algorithms.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/data/optimization_algorithms.rda -------------------------------------------------------------------------------- /data/tennis_agresti.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/data/tennis_agresti.rda -------------------------------------------------------------------------------- /figure/unnamed-chunk-151-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/figure/unnamed-chunk-151-1.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-152-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/figure/unnamed-chunk-152-1.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-155-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/figure/unnamed-chunk-155-1.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-156-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/figure/unnamed-chunk-156-1.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-159-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/figure/unnamed-chunk-159-1.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-163-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/figure/unnamed-chunk-163-1.png -------------------------------------------------------------------------------- /inst/CITATION: -------------------------------------------------------------------------------- 1 | citHeader("To cite bpc in publications use:") 2 | 3 | citEntry( 4 | entry = "Misc", 5 | title = "{bpc}: A package for Bayesian Paired Comparison analysis", 6 | author = c(person(given = "David", family = "Issa Mattos"), 7 | person(given = "Erika", family = "Martins Silva Ramos")), 8 | journal = "", 9 | year = "2020", 10 | volume = "", 11 | number = "", 12 | pages = "", 13 | url = "", 14 | textVersion = paste( 15 | 16 | ) 17 | ) 18 | -------------------------------------------------------------------------------- /inst/DockerImages/Dockerfiles: -------------------------------------------------------------------------------- 1 | FROM rocker/verse:4.0.3 2 | MAINTAINER David Issa Mattos (issamattos.david@gmail.com) 3 | 4 | RUN apt-get update -qq \ 5 | && apt-get -y install build-essential libv8-dev ed pkg-config apt-utils libglu1-mesa-dev libnlopt-dev nano libgsl-dev libz-dev 6 | 7 | RUN mkdir -p $HOME/.R/ \ 8 | && echo "CXX14FLAGS=-O3 -march=native -mtune=native -fPIC" >> $HOME/.R/Makevars \ 9 | && echo "CXX14=g++ -std=c++11" >> $HOME/.R/Makevars \ 10 | && echo "CXX14FLAGS+= -std=c++11" >> $HOME/.R/Makevars \ 11 | && echo "CXX14STD='-std=c++11'" >> $HOME/.R/Makevars \ 12 | && echo "rstan::rstan_options(auto_write = TRUE)" >> /home/rstudio/.Rprofile \ 13 | && echo "options(mc.cores = parallel::detectCores())" >> /home/rstudio/.Rprofile 14 | 15 | RUN install2.r --error rstan 16 | 17 | RUN install2.r --error \ 18 | && Rscript -e 'remotes::install_github('davidissamattos/bpcs')' -------------------------------------------------------------------------------- /inst/logo/boxing2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/inst/logo/boxing2.png -------------------------------------------------------------------------------- /inst/logo/create_logo.R: -------------------------------------------------------------------------------- 1 | library(showtext) 2 | library(hexSticker) 3 | font_add_google("Noto Sans JP", "sans-serif") 4 | ## Automatically use showtext to render text for future devices 5 | showtext_auto() 6 | 7 | img<-system.file('inst','logo','boxing2.png', package = 'bpcs') 8 | 9 | plot(sticker(img, 10 | package="bpcs", 11 | p_size=8, 12 | p_y=1.55, 13 | p_color = "#EBEBEB", 14 | s_x=1, 15 | s_y=0.8, 16 | s_width=0.6, 17 | s_height =0.6, 18 | h_fill="#525252", 19 | h_color="#1F1F1F", 20 | filename="inst/logo/logo.png")) 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /inst/logo/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/inst/logo/logo.png -------------------------------------------------------------------------------- /inst/stan/license.stan: -------------------------------------------------------------------------------- 1 | /* 2 | bpc is free software: you can redistribute it and/or modify 3 | it under the terms of the GNU General Public License as published by 4 | the Free Software Foundation, either version 3 of the License, or 5 | (at your option) any later version. 6 | 7 | bpc is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with bpc. If not, see . 14 | */ 15 | -------------------------------------------------------------------------------- /inst/testdata/test_bt.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/inst/testdata/test_bt.rda -------------------------------------------------------------------------------- /inst/testdata/test_btU.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/inst/testdata/test_btU.rda -------------------------------------------------------------------------------- /inst/testdata/test_btUordereffect.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/inst/testdata/test_btUordereffect.rda -------------------------------------------------------------------------------- /inst/testdata/test_bt_subject.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/inst/testdata/test_bt_subject.rda -------------------------------------------------------------------------------- /inst/testdata/test_btorder.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/inst/testdata/test_btorder.rda -------------------------------------------------------------------------------- /inst/testdata/test_btscores.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/inst/testdata/test_btscores.rda -------------------------------------------------------------------------------- /inst/testdata/test_davidson.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/inst/testdata/test_davidson.rda -------------------------------------------------------------------------------- /inst/testdata/test_davidsonU.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/inst/testdata/test_davidsonU.rda -------------------------------------------------------------------------------- /inst/testdata/test_davidsonorder.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/inst/testdata/test_davidsonorder.rda -------------------------------------------------------------------------------- /inst/testdata/test_davidsonscores.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/inst/testdata/test_davidsonscores.rda -------------------------------------------------------------------------------- /inst/testdata/test_predictors.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/inst/testdata/test_predictors.rda -------------------------------------------------------------------------------- /man/HPD_higher_from_column.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_hpd.R 3 | \name{HPD_higher_from_column} 4 | \alias{HPD_higher_from_column} 5 | \title{Returns the higher value of the HPD interval for a data frame column} 6 | \usage{ 7 | HPD_higher_from_column(column, credMass = 0.95) 8 | } 9 | \arguments{ 10 | \item{column}{the data to calculate the HPDI} 11 | 12 | \item{credMass}{Credibility mass for the interval (area contained in the interval)} 13 | } 14 | \value{ 15 | the value of the higher HPD interval for that column 16 | } 17 | \description{ 18 | Returns the higher value of the HPD interval for a data frame column 19 | } 20 | \references{ 21 | Mike Meredith and John Kruschke (2020). HDInterval: Highest (Posterior) Density Intervals. R package version 0.2.2. https://CRAN.R-project.org/package=HDInterval 22 | } 23 | -------------------------------------------------------------------------------- /man/HPD_lower_from_column.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_hpd.R 3 | \name{HPD_lower_from_column} 4 | \alias{HPD_lower_from_column} 5 | \title{Returns the lower value of the HPD interval for a data frame column} 6 | \usage{ 7 | HPD_lower_from_column(column, credMass = 0.95) 8 | } 9 | \arguments{ 10 | \item{column}{the data to calculate the HPDI} 11 | 12 | \item{credMass}{Credibility mass for the interval (area contained in the interval)} 13 | } 14 | \value{ 15 | the value of the lower HPD interval for that column 16 | } 17 | \description{ 18 | Returns the lower value of the HPD interval for a data frame column 19 | } 20 | \references{ 21 | Mike Meredith and John Kruschke (2020). HDInterval: Highest (Posterior) Density Intervals. R package version 0.2.2. https://CRAN.R-project.org/package=HDInterval 22 | } 23 | -------------------------------------------------------------------------------- /man/bernoulli_rng.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_btpredict.R 3 | \name{bernoulli_rng} 4 | \alias{bernoulli_rng} 5 | \title{Bernoulli random number distribution} 6 | \usage{ 7 | bernoulli_rng(n, p) 8 | } 9 | \arguments{ 10 | \item{n}{the size to return} 11 | 12 | \item{p}{probability} 13 | } 14 | \value{ 15 | a random number 1 or 0 16 | } 17 | \description{ 18 | Bernoulli random number distribution 19 | } 20 | -------------------------------------------------------------------------------- /man/bpcs-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpcs-package.R 3 | \docType{package} 4 | \name{bpcs-package} 5 | \alias{bpcs-package} 6 | \title{bpcs - A package for Bayesian Paired Comparison analysis with Stan} 7 | \description{ 8 | bpcs - A package for Bayesian Paired Comparison analysis with Stan 9 | } 10 | \references{ 11 | Stan Development Team (2020). RStan: the R interface to Stan. R package version 2.21.2. https://mc-stan.org 12 | } 13 | -------------------------------------------------------------------------------- /man/brasil_soccer_league.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{brasil_soccer_league} 5 | \alias{brasil_soccer_league} 6 | \title{This is a dataset with the results matches fromo the first league of the Brazilian soccer championship from 2017-2019. 7 | It was reduced and translatedfrom the adaduque/Brasileirao_Dataset repository} 8 | \format{ 9 | Data frame that contains 1140 matches and 9 Columns from the Brazilian soccer championship 10 | \itemize{ 11 | \item Time: time of the day in 24h format 12 | \item DayWeek: day of the week 13 | \item Date: date YY-MM-DD 14 | \item HomeTeam: name of the team playing home 15 | \item VisitorTeam: name of the team playing visitor 16 | \item Round: Round number of the championship 17 | \item Stadium: Name of the stadium where the game was played 18 | \item ScoreHomeTeam: number of goals for the home team 19 | \item ScoreVisitorTeam: number of goals for the visitor 20 | } 21 | } 22 | \source{ 23 | \url{https://github.com/adaoduque/Brasileirao_Dataset} 24 | } 25 | \usage{ 26 | brasil_soccer_league 27 | } 28 | \description{ 29 | This is a dataset with the results matches fromo the first league of the Brazilian soccer championship from 2017-2019. 30 | It was reduced and translatedfrom the adaduque/Brasileirao_Dataset repository 31 | } 32 | \keyword{data} 33 | -------------------------------------------------------------------------------- /man/btpredict.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_btpredict.R 3 | \name{btpredict} 4 | \alias{btpredict} 5 | \title{This function emulates the generate quantities from stan that is not working in some cases} 6 | \usage{ 7 | btpredict(standata, draws) 8 | } 9 | \arguments{ 10 | \item{standata}{a list containing the data from stan} 11 | 12 | \item{draws}{a matrix of draws from the parameters of the sampled model} 13 | } 14 | \value{ 15 | a matrix with each column represents a posterior prediction of the data and each row a predicted value 16 | } 17 | \description{ 18 | This function emulates the generate quantities from stan that is not working in some cases 19 | } 20 | -------------------------------------------------------------------------------- /man/calculate_p1_win_and_ties.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_btpredict.R 3 | \name{calculate_p1_win_and_ties} 4 | \alias{calculate_p1_win_and_ties} 5 | \title{Calcutate the probability for a single row} 6 | \usage{ 7 | calculate_p1_win_and_ties(i, standata, parameters, n) 8 | } 9 | \arguments{ 10 | \item{i}{row number} 11 | 12 | \item{standata}{input standata} 13 | 14 | \item{parameters}{input sampled parameters} 15 | 16 | \item{n}{number of draws} 17 | } 18 | \value{ 19 | an array with probability of win1 and probability of tie 20 | } 21 | \description{ 22 | Calcutate the probability for a single row 23 | } 24 | -------------------------------------------------------------------------------- /man/calculate_prob_from_vector.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers.R 3 | \name{calculate_prob_from_vector} 4 | \alias{calculate_prob_from_vector} 5 | \title{Calculate the probability of occurrence of a in a vector x} 6 | \usage{ 7 | calculate_prob_from_vector(x, a) 8 | } 9 | \arguments{ 10 | \item{x}{a vector} 11 | 12 | \item{a}{a value in x} 13 | } 14 | \value{ 15 | a value of the probability of a in vector x 16 | } 17 | \description{ 18 | Calculate the probability of occurrence of a in a vector x 19 | } 20 | -------------------------------------------------------------------------------- /man/check_convergence_diagnostics.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_exports.R 3 | \name{check_convergence_diagnostics} 4 | \alias{check_convergence_diagnostics} 5 | \title{Run cmdstan diagnostics for convergence and print the results in the screen 6 | Thin wrapper over cmdstanr cmdstan_diagnose() function} 7 | \usage{ 8 | check_convergence_diagnostics(bpc_object) 9 | } 10 | \arguments{ 11 | \item{bpc_object}{a bpc object} 12 | } 13 | \description{ 14 | Run cmdstan diagnostics for convergence and print the results in the screen 15 | Thin wrapper over cmdstanr cmdstan_diagnose() function 16 | } 17 | \examples{ 18 | \donttest{ 19 | m<-bpc(data = tennis_agresti, 20 | player0 = 'player0', 21 | player1 = 'player1', 22 | result_column = 'y', 23 | model_type = 'bt', 24 | solve_ties = 'none') 25 | check_convergence_diagnostics(m) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /man/check_if_there_are_na.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_checks.R 3 | \name{check_if_there_are_na} 4 | \alias{check_if_there_are_na} 5 | \title{Check for NA in the specfic columns and returns T or F is there is at least 1 NA in those columns} 6 | \usage{ 7 | check_if_there_are_na( 8 | d, 9 | player0, 10 | player1, 11 | player0_score = NULL, 12 | player1_score = NULL, 13 | result_column = NULL 14 | ) 15 | } 16 | \arguments{ 17 | \item{d}{a data frame} 18 | 19 | \item{player0}{the name of column for player0} 20 | 21 | \item{player1}{the name of column for player1} 22 | 23 | \item{player0_score}{the name of column for player0 scores} 24 | 25 | \item{player1_score}{the name of column for player1 scores} 26 | 27 | \item{result_column}{the name of column for results} 28 | } 29 | \value{ 30 | TRUE (there are NA) or FALSE (no NA) 31 | } 32 | \description{ 33 | Check for NA in the specfic columns and returns T or F is there is at least 1 NA in those columns 34 | } 35 | -------------------------------------------------------------------------------- /man/check_if_there_are_ties.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_checks.R 3 | \name{check_if_there_are_ties} 4 | \alias{check_if_there_are_ties} 5 | \title{Check if a data frame column contains ties} 6 | \usage{ 7 | check_if_there_are_ties(d_column) 8 | } 9 | \arguments{ 10 | \item{d_column}{a column with the values for the ties} 11 | } 12 | \value{ 13 | T (there are ties) or F (no ties) 14 | } 15 | \description{ 16 | Check if a data frame column contains ties 17 | } 18 | -------------------------------------------------------------------------------- /man/check_numeric_predictor_matrix.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_checks.R 3 | \name{check_numeric_predictor_matrix} 4 | \alias{check_numeric_predictor_matrix} 5 | \title{Check if all values in the predictor matrix are numeric and not NA. Note that TRUE will be cast to 1 and FALSE will be cast to 0} 6 | \usage{ 7 | check_numeric_predictor_matrix(predictor_matrix) 8 | } 9 | \arguments{ 10 | \item{predictor_matrix}{a predictor matrix generated by the create_predictor_matrix_with_player_lookup_table function} 11 | } 12 | \value{ 13 | TRUE (correct) or FALSE (with problems) 14 | } 15 | \description{ 16 | Check if all values in the predictor matrix are numeric and not NA. Note that TRUE will be cast to 1 and FALSE will be cast to 0 17 | } 18 | -------------------------------------------------------------------------------- /man/check_predictors_df_contains_all_players.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_checks.R 3 | \name{check_predictors_df_contains_all_players} 4 | \alias{check_predictors_df_contains_all_players} 5 | \title{Check if the predictor df contains all players and only those} 6 | \usage{ 7 | check_predictors_df_contains_all_players(predictor_df, lookup_table) 8 | } 9 | \arguments{ 10 | \item{predictor_df}{the predictor input data frame} 11 | 12 | \item{lookup_table}{a lookup table of the players} 13 | } 14 | \value{ 15 | TRUE (correct) or FALSE (with problems) 16 | } 17 | \description{ 18 | Check if the predictor df contains all players and only those 19 | } 20 | -------------------------------------------------------------------------------- /man/check_result_column.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_checks.R 3 | \name{check_result_column} 4 | \alias{check_result_column} 5 | \title{Check if a data frame column contains only the values 1 0 and 2. Used to check the format of the results} 6 | \usage{ 7 | check_result_column(d_column) 8 | } 9 | \arguments{ 10 | \item{d_column}{a column from a data frame} 11 | } 12 | \value{ 13 | TRUE (correct) or FALSE (with problems) 14 | } 15 | \description{ 16 | Check if a data frame column contains only the values 1 0 and 2. Used to check the format of the results 17 | } 18 | -------------------------------------------------------------------------------- /man/check_z_column.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_checks.R 3 | \name{check_z_column} 4 | \alias{check_z_column} 5 | \title{Check if a data frame column contains only the values 1 or 0. For the z column} 6 | \usage{ 7 | check_z_column(d_column) 8 | } 9 | \arguments{ 10 | \item{d_column}{a column of a data frame to be tested} 11 | } 12 | \value{ 13 | TRUE (correct) or FALSE (with problems) 14 | } 15 | \description{ 16 | Check if a data frame column contains only the values 1 or 0. For the z column 17 | } 18 | -------------------------------------------------------------------------------- /man/compute_scores.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers.R 3 | \name{compute_scores} 4 | \alias{compute_scores} 5 | \title{Giving a player0 an player1 scores, this functions adds one column to the data frame containing who won (0= player0 1=player1 2=tie) and another if it was a tie. 6 | The ties column superseeds the y column. 7 | If it was tie the y column does not matter 8 | y column: (0= player0 1=player1 2=tie) 9 | ties column (0=not tie, 1=tie)} 10 | \usage{ 11 | compute_scores( 12 | d, 13 | player0_score, 14 | player1_score, 15 | solve_ties = "random", 16 | win_score = "higher" 17 | ) 18 | } 19 | \arguments{ 20 | \item{d}{dataframe} 21 | 22 | \item{player0_score}{name of the column in data} 23 | 24 | \item{player1_score}{name of the column in data} 25 | 26 | \item{solve_ties}{Method to solve the ties, either randomly allocate, or do nothing, or remove the row from the datasetc('random', 'none', 'remove').} 27 | 28 | \item{win_score}{decides if who wins is the one that has the highest score or the lowest score} 29 | } 30 | \value{ 31 | a dataframe with column 'y' that contains the results of the comparison and a ties column indicating if there was ties 32 | } 33 | \description{ 34 | Giving a player0 an player1 scores, this functions adds one column to the data frame containing who won (0= player0 1=player1 2=tie) and another if it was a tie. 35 | The ties column superseeds the y column. 36 | If it was tie the y column does not matter 37 | y column: (0= player0 1=player1 2=tie) 38 | ties column (0=not tie, 1=tie) 39 | } 40 | -------------------------------------------------------------------------------- /man/compute_ties.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers.R 3 | \name{compute_ties} 4 | \alias{compute_ties} 5 | \title{Giving a result column we create a new column with ties (0 and 1 if it has)} 6 | \usage{ 7 | compute_ties(d, result_column) 8 | } 9 | \arguments{ 10 | \item{d}{data frame} 11 | 12 | \item{result_column}{column where the result is} 13 | } 14 | \value{ 15 | dataframe with a column called ties 16 | } 17 | \description{ 18 | Giving a result column we create a new column with ties (0 and 1 if it has) 19 | } 20 | -------------------------------------------------------------------------------- /man/create_array_of_par_names.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_indexes.R 3 | \name{create_array_of_par_names} 4 | \alias{create_array_of_par_names} 5 | \title{Create an array with the parameter name and to what player/cluster it refers to in the order stan presents} 6 | \usage{ 7 | create_array_of_par_names( 8 | par, 9 | lookup_table, 10 | cluster_lookup_table = NULL, 11 | subject_predictors_lookup_table = NULL, 12 | keep_par_name = T 13 | ) 14 | } 15 | \arguments{ 16 | \item{par}{name of the parameter} 17 | 18 | \item{lookup_table}{lookup table of the players} 19 | 20 | \item{cluster_lookup_table}{a list of lookup table of the clusters} 21 | 22 | \item{subject_predictors_lookup_table}{a subject predictor lookup table} 23 | 24 | \item{keep_par_name}{keep the parameter name e.g. lambda Graff instead of Graff. Default to T. Only valid for lambda, so we can have better ranks} 25 | } 26 | \value{ 27 | a data. frame where we change the names in the variable column to the corresponding parameter_name from the lookup table 28 | } 29 | \description{ 30 | Create an array with the parameter name and to what player/cluster it refers to in the order stan presents 31 | } 32 | -------------------------------------------------------------------------------- /man/create_bpc_object.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_object.R 3 | \name{create_bpc_object} 4 | \alias{create_bpc_object} 5 | \title{Defines the class bpc and creates the bpc object. 6 | To create we need to receive some defined parameters (the arguments from the bpc function), a lookup table and a the stanfit 7 | object generated from the rstan sampling procedure} 8 | \usage{ 9 | create_bpc_object( 10 | fit, 11 | lookup_table, 12 | model_type, 13 | standata, 14 | call_arg, 15 | output_dir, 16 | cluster_lookup_table = NULL, 17 | predictors_df = NULL, 18 | predictors_lookup_table = NULL, 19 | predictors_matrix = NULL, 20 | subject_predictors_lookup_table = NULL, 21 | subject_predictors_matrix = NULL, 22 | used_pars = "lambda" 23 | ) 24 | } 25 | \arguments{ 26 | \item{fit}{A fit object from cmdstanr} 27 | 28 | \item{lookup_table}{lookup_table dataframe. Two columns one Index the other Names where each each index will match a string in the names} 29 | 30 | \item{model_type}{the type of the model used to call stan (string)} 31 | 32 | \item{standata}{a list with the data used to call the rstan::sampling procedure} 33 | 34 | \item{call_arg}{a list with the arguments called from the bpc function} 35 | 36 | \item{output_dir}{output directory for the csv files} 37 | 38 | \item{cluster_lookup_table}{a lookup table with we have random effects} 39 | 40 | \item{predictors_df}{the data frame of the predictors for a generalized model} 41 | 42 | \item{predictors_lookup_table}{a lookup table for generalized models} 43 | 44 | \item{predictors_matrix}{a matrix of predictors for generalized models} 45 | 46 | \item{subject_predictors_lookup_table}{a lookup table for the subject predictors models} 47 | 48 | \item{subject_predictors_matrix}{a matrix of predictors for the subject predictors matrix} 49 | 50 | \item{used_pars}{an array with all the parameters set for the model} 51 | } 52 | \value{ 53 | a bpc object 54 | } 55 | \description{ 56 | Defines the class bpc and creates the bpc object. 57 | To create we need to receive some defined parameters (the arguments from the bpc function), a lookup table and a the stanfit 58 | object generated from the rstan sampling procedure 59 | } 60 | -------------------------------------------------------------------------------- /man/create_cluster_index.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_indexes.R 3 | \name{create_cluster_index} 4 | \alias{create_cluster_index} 5 | \title{Create two columns with the indexes for the names of the players 6 | Here we create a new lookup table. Should be used when sampling the parameters} 7 | \usage{ 8 | create_cluster_index(d, cluster, i) 9 | } 10 | \arguments{ 11 | \item{d}{A data frame containing the observations. The other parameters specify the name of the columns} 12 | 13 | \item{cluster}{The name of the column of data data contains player0} 14 | 15 | \item{i}{number of the cluster (1 or 2)} 16 | } 17 | \value{ 18 | A dataframe with the additional columns 'cluster_index' 19 | } 20 | \description{ 21 | Create two columns with the indexes for the names of the players 22 | Here we create a new lookup table. Should be used when sampling the parameters 23 | } 24 | -------------------------------------------------------------------------------- /man/create_cluster_index_with_existing_lookup_table.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_indexes.R 3 | \name{create_cluster_index_with_existing_lookup_table} 4 | \alias{create_cluster_index_with_existing_lookup_table} 5 | \title{Create two columns with the indexes for the names 6 | Here we use an existing lookup table. Should be used in predicting} 7 | \usage{ 8 | create_cluster_index_with_existing_lookup_table( 9 | d, 10 | cluster, 11 | cluster_lookup_table, 12 | i 13 | ) 14 | } 15 | \arguments{ 16 | \item{d}{A data frame containing the observations. The other parameters specify the name of the columns} 17 | 18 | \item{cluster}{The name of the column of data data contains player0} 19 | 20 | \item{cluster_lookup_table}{a lookup table for the cluster} 21 | 22 | \item{i}{index of the cluster in the list (1, 2, 3)} 23 | } 24 | \value{ 25 | A dataframe with the additional columns 'player0_index' and 'player1_index' that contains the indexes 26 | } 27 | \description{ 28 | Create two columns with the indexes for the names 29 | Here we use an existing lookup table. Should be used in predicting 30 | } 31 | -------------------------------------------------------------------------------- /man/create_index.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_indexes.R 3 | \name{create_index} 4 | \alias{create_index} 5 | \title{Create two columns with the indexes for the names of the players 6 | Here we create a new lookup table. Should be used when sampling the parameters} 7 | \usage{ 8 | create_index(d, player0, player1) 9 | } 10 | \arguments{ 11 | \item{d}{A data frame containing the observations. The other parameters specify the name of the columns} 12 | 13 | \item{player0}{The name of the column of data data contains player0} 14 | 15 | \item{player1}{The name of the column of data data contains player0} 16 | } 17 | \value{ 18 | A dataframe with the additional columns 'player0_index' and 'player1_index' that contains the indexes 19 | } 20 | \description{ 21 | Create two columns with the indexes for the names of the players 22 | Here we create a new lookup table. Should be used when sampling the parameters 23 | } 24 | -------------------------------------------------------------------------------- /man/create_index_cluster_lookuptable.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_indexes.R 3 | \name{create_index_cluster_lookuptable} 4 | \alias{create_index_cluster_lookuptable} 5 | \title{Create a lookup table of names and indexes 6 | Note that the indexes will be created in the order they appear. For string this does not make much difference but for numbers the index might be different than the actual number that appears in names} 7 | \usage{ 8 | create_index_cluster_lookuptable(d, cluster) 9 | } 10 | \arguments{ 11 | \item{d}{A data frame containing the observations. The other parameters specify the name of the columns} 12 | 13 | \item{cluster}{A string with the name of the cluster variable} 14 | } 15 | \value{ 16 | A dataframe of a lookup table with columns Names and Index 17 | } 18 | \description{ 19 | Create a lookup table of names and indexes 20 | Note that the indexes will be created in the order they appear. For string this does not make much difference but for numbers the index might be different than the actual number that appears in names 21 | } 22 | -------------------------------------------------------------------------------- /man/create_index_lookuptable.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_indexes.R 3 | \name{create_index_lookuptable} 4 | \alias{create_index_lookuptable} 5 | \title{Create a lookup table of names and indexes 6 | Note that the indexes will be created in the order they appear. For string this doesnt make much difference but for numbers the index might be different than the actual number that appears in names} 7 | \usage{ 8 | create_index_lookuptable(d, player0, player1) 9 | } 10 | \arguments{ 11 | \item{d}{A data frame containing the observations. The other parameters specify the name of the columns} 12 | 13 | \item{player0}{The name of the column of data contains player0} 14 | 15 | \item{player1}{The name of the column of data contains player0} 16 | } 17 | \value{ 18 | A dataframe of a lookup table with columns Names and Index 19 | } 20 | \description{ 21 | Create a lookup table of names and indexes 22 | Note that the indexes will be created in the order they appear. For string this doesnt make much difference but for numbers the index might be different than the actual number that appears in names 23 | } 24 | -------------------------------------------------------------------------------- /man/create_index_predictors_with_lookup_table.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_indexes.R 3 | \name{create_index_predictors_with_lookup_table} 4 | \alias{create_index_predictors_with_lookup_table} 5 | \title{Receives one column with player names and returns a data frame with the relevant index columns based on a given lookup table 6 | To be used with the predictors data frame} 7 | \usage{ 8 | create_index_predictors_with_lookup_table(d, player, lookup_table) 9 | } 10 | \arguments{ 11 | \item{d}{a data frame of the predictors} 12 | 13 | \item{player}{The name of the column of data data contains the player} 14 | 15 | \item{lookup_table}{a lookup table data frame} 16 | } 17 | \value{ 18 | A dataframe with the additional column 'player_index' 19 | } 20 | \description{ 21 | Receives one column with player names and returns a data frame with the relevant index columns based on a given lookup table 22 | To be used with the predictors data frame 23 | } 24 | -------------------------------------------------------------------------------- /man/create_index_with_existing_lookup_table.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_indexes.R 3 | \name{create_index_with_existing_lookup_table} 4 | \alias{create_index_with_existing_lookup_table} 5 | \title{Create two columns with the indexes for the names 6 | Here we use an existing lookup table. Should be used in predicting} 7 | \usage{ 8 | create_index_with_existing_lookup_table(d, player0, player1, lookup_table) 9 | } 10 | \arguments{ 11 | \item{d}{A data frame containing the observations. The other parameters specify the name of the columns} 12 | 13 | \item{player0}{The name of the column of data data contains player0} 14 | 15 | \item{player1}{The name of the column of data data contains player0} 16 | 17 | \item{lookup_table}{lookup_table a lookup table data frame} 18 | } 19 | \value{ 20 | A dataframe with the additional columns 'player0_index' and 'player1_index' that contains the indexes 21 | } 22 | \description{ 23 | Create two columns with the indexes for the names 24 | Here we use an existing lookup table. Should be used in predicting 25 | } 26 | -------------------------------------------------------------------------------- /man/create_predictor_matrix_with_player_lookup_table.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_indexes.R 3 | \name{create_predictor_matrix_with_player_lookup_table} 4 | \alias{create_predictor_matrix_with_player_lookup_table} 5 | \title{Receives a predictor dataframe, a string with the column of the player, a vector of strings with the columns for the predictors and a lookup table and returns an ordered matrix for Stan 6 | To be used with the predictors data frame} 7 | \usage{ 8 | create_predictor_matrix_with_player_lookup_table( 9 | d, 10 | player, 11 | predictors_columns, 12 | lookup_table 13 | ) 14 | } 15 | \arguments{ 16 | \item{d}{a data frame of the predictors} 17 | 18 | \item{player}{The name of the column of data data contains the player} 19 | 20 | \item{predictors_columns}{a vector with strings containing the columns for the predictors} 21 | 22 | \item{lookup_table}{a lookup table data frame} 23 | } 24 | \value{ 25 | A matrix to be used in stan 26 | } 27 | \description{ 28 | Receives a predictor dataframe, a string with the column of the player, a vector of strings with the columns for the predictors and a lookup table and returns an ordered matrix for Stan 29 | To be used with the predictors data frame 30 | } 31 | -------------------------------------------------------------------------------- /man/create_predictors_lookup_table.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_indexes.R 3 | \name{create_predictors_lookup_table} 4 | \alias{create_predictors_lookup_table} 5 | \title{Receives a vector with predictors strings (the column names) and returns a predictor_lookup_table} 6 | \usage{ 7 | create_predictors_lookup_table(predictors_columns) 8 | } 9 | \arguments{ 10 | \item{predictors_columns}{a vector with strings containing the columns for the predictors} 11 | } 12 | \value{ 13 | A matrix to be used in stan 14 | } 15 | \description{ 16 | Receives a vector with predictors strings (the column names) and returns a predictor_lookup_table 17 | } 18 | -------------------------------------------------------------------------------- /man/create_subject_predictor_lookuptable.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_indexes.R 3 | \name{create_subject_predictor_lookuptable} 4 | \alias{create_subject_predictor_lookuptable} 5 | \title{Create a lookup table for the subject Predictors} 6 | \usage{ 7 | create_subject_predictor_lookuptable(subject_predictors) 8 | } 9 | \arguments{ 10 | \item{subject_predictors}{a vectore containing the names of the subject predictors} 11 | } 12 | \value{ 13 | a dataframe with a lookup table 14 | } 15 | \description{ 16 | Create a lookup table for the subject Predictors 17 | } 18 | -------------------------------------------------------------------------------- /man/expand_aggregated_data.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_data_transformation.R 3 | \name{expand_aggregated_data} 4 | \alias{expand_aggregated_data} 5 | \title{Expand aggregated data 6 | Several datasets for the Bradley-Terry Model aggregate the number of wins for each player in a different column. 7 | The models we provide are intended to be used in a long format. A single result for each contest. 8 | This function expands datasets that have aggregated data into this long format.} 9 | \usage{ 10 | expand_aggregated_data( 11 | d, 12 | player0, 13 | player1, 14 | wins0, 15 | wins1, 16 | ties = NULL, 17 | keep = NULL 18 | ) 19 | } 20 | \arguments{ 21 | \item{d}{a data frame} 22 | 23 | \item{player0}{string with column name of player0} 24 | 25 | \item{player1}{string with column name of player1} 26 | 27 | \item{wins0}{string with column name of the number of wins of player 0} 28 | 29 | \item{wins1}{string with column name of the number of wins of player 1} 30 | 31 | \item{ties}{string with the column of the ties} 32 | 33 | \item{keep}{an array of strings with the name of columns we want to keep in the new data frame (and repeat in every expanded row)} 34 | } 35 | \value{ 36 | a data frame with the expanded dataset. It will have the columns player1, player0, y, the keep columns, and a rowid column (to make each row unique) 37 | } 38 | \description{ 39 | Expand aggregated data 40 | Several datasets for the Bradley-Terry Model aggregate the number of wins for each player in a different column. 41 | The models we provide are intended to be used in a long format. A single result for each contest. 42 | This function expands datasets that have aggregated data into this long format. 43 | } 44 | \examples{ 45 | #Creating a simple data frame with only one row to illustrate how the function works 46 | df1 <- tibble::tribble(~player0, ~player1, ~wins0, ~wins1,~cluster, 'A','B',4, 3, 'c1') 47 | df2 <- expand_aggregated_data(df1,'player0', 'player1', 'wins0', 'wins1', keep=c('cluster')) 48 | print(df2) 49 | } 50 | -------------------------------------------------------------------------------- /man/figures/README-unnamed-chunk-7-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/man/figures/README-unnamed-chunk-7-1.png -------------------------------------------------------------------------------- /man/figures/README-unnamed-chunk-8-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/man/figures/README-unnamed-chunk-8-1.png -------------------------------------------------------------------------------- /man/figures/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/man/figures/logo.png -------------------------------------------------------------------------------- /man/fix_ties.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers.R 3 | \name{fix_ties} 4 | \alias{fix_ties} 5 | \title{fix_ties 6 | This functions provides the possible solutions for ties in the data frame} 7 | \usage{ 8 | fix_ties(d, solve_ties = "random") 9 | } 10 | \arguments{ 11 | \item{d}{a dataframe with the results in the y column} 12 | 13 | \item{solve_ties}{method for solving ties 'random', 'none', or 'remove'} 14 | } 15 | \value{ 16 | a data frame with the result column y adjusted 17 | } 18 | \description{ 19 | fix_ties 20 | This functions provides the possible solutions for ties in the data frame 21 | } 22 | -------------------------------------------------------------------------------- /man/get_fit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_exports.R 3 | \name{get_fit} 4 | \alias{get_fit} 5 | \title{Retrieve the cmstanr fit object 6 | This object can be used with any other function or package. You can also convert it to a stanfit objects from rstan} 7 | \usage{ 8 | get_fit(bpc_object) 9 | } 10 | \arguments{ 11 | \item{bpc_object}{a bpc object} 12 | } 13 | \value{ 14 | a cmdstanr fit object 15 | } 16 | \description{ 17 | Retrieve the cmstanr fit object 18 | This object can be used with any other function or package. You can also convert it to a stanfit objects from rstan 19 | } 20 | \examples{ 21 | \donttest{ 22 | m<-bpc(data = tennis_agresti, 23 | player0 = 'player0', 24 | player1 = 'player1', 25 | result_column = 'y', 26 | model_type = 'bt', 27 | solve_ties = 'none') 28 | fit<- get_fit(m) 29 | print(class(fit)) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /man/get_loo.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_exports.R 3 | \name{get_loo} 4 | \alias{get_loo} 5 | \title{Tiny wrapper for the PSIS-LOO-CV method from the loo package.} 6 | \usage{ 7 | get_loo(bpc_object) 8 | } 9 | \arguments{ 10 | \item{bpc_object}{a bpc object} 11 | } 12 | \value{ 13 | a loo object 14 | } 15 | \description{ 16 | This is used to evaluate the fit of the model using entropy criteria 17 | } 18 | \examples{ 19 | \donttest{ 20 | m<-bpc(data = tennis_agresti, 21 | player0 = 'player0', 22 | player1 = 'player1', 23 | result_column = 'y', 24 | model_type = 'bt', 25 | solve_ties = 'none') 26 | l<-get_loo(m) 27 | print(l) 28 | } 29 | } 30 | \references{ 31 | Vehtari A, Gelman A, Gabry J (2017). Practical Bayesian model evaluation using leave-one-out cross-validation and WAIC. Statistics and Computing_, 27, 1413-1432 32 | } 33 | -------------------------------------------------------------------------------- /man/get_model_parameters.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers.R 3 | \name{get_model_parameters} 4 | \alias{get_model_parameters} 5 | \title{Return all the name of the used parameters in a model from a bpc_object. 6 | Here we exclude the log_lik and the lp__ since they are not parameters of the model} 7 | \usage{ 8 | get_model_parameters(bpc_object) 9 | } 10 | \arguments{ 11 | \item{bpc_object}{a bpc object} 12 | } 13 | \value{ 14 | a vector with the name of the parameters 15 | } 16 | \description{ 17 | Return all the name of the used parameters in a model from a bpc_object. 18 | Here we exclude the log_lik and the lp__ since they are not parameters of the model 19 | } 20 | -------------------------------------------------------------------------------- /man/get_output_dir.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_exports.R 3 | \name{get_output_dir} 4 | \alias{get_output_dir} 5 | \title{Get the location of the output directory for the cmdstanr csv fukes} 6 | \usage{ 7 | get_output_dir(bpc_object) 8 | } 9 | \arguments{ 10 | \item{bpc_object}{a bpc object} 11 | } 12 | \description{ 13 | Get the location of the output directory for the cmdstanr csv fukes 14 | } 15 | -------------------------------------------------------------------------------- /man/get_parameters.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_get_parameters.R 3 | \name{get_parameters} 4 | \alias{get_parameters} 5 | \title{Return the mean and the HPDI/credible intervals of the parameters of the model} 6 | \usage{ 7 | get_parameters( 8 | bpc_object, 9 | params = NULL, 10 | HPDI = TRUE, 11 | credMass = 0.95, 12 | n_eff = FALSE, 13 | Rhat = FALSE, 14 | keep_par_name = T 15 | ) 16 | } 17 | \arguments{ 18 | \item{bpc_object}{a bpc object} 19 | 20 | \item{params}{a vector with the parameters to use. If null then all will be present} 21 | 22 | \item{HPDI}{should return the HPDI or the credible intervals. Default is returning the HPDI} 23 | 24 | \item{credMass}{probability mass for the summary stats} 25 | 26 | \item{n_eff}{Should include the number of effective samples in the df} 27 | 28 | \item{Rhat}{Should include the Rhat in the df} 29 | 30 | \item{keep_par_name}{keep the parameter name e.g. lambda Graff instead of Graff. Default to T. Only valid for lambda, so we can have better ranks} 31 | } 32 | \value{ 33 | a data frame containing a column with the parameters, a column with mean and two columns with higher and lower intervals 34 | } 35 | \description{ 36 | Return a data frame with the mean and with high and low 95\% hpd interval or credible interval for all parameters of the model 37 | } 38 | \examples{ 39 | \donttest{ 40 | m<-bpc(data = tennis_agresti, 41 | player0 = 'player0', 42 | player1 = 'player1', 43 | result_column = 'y', 44 | model_type = 'bt', 45 | solve_ties = 'none') 46 | hpdi<-get_parameters(m) 47 | print(hpdi) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /man/get_parameters_df.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_get_parameters.R 3 | \name{get_parameters_df} 4 | \alias{get_parameters_df} 5 | \title{Function to retrieve a data frame with summary of the parameters} 6 | \usage{ 7 | get_parameters_df( 8 | bpc_object, 9 | params = NULL, 10 | HPDI = TRUE, 11 | credMass = 0.95, 12 | n_eff = FALSE, 13 | Rhat = FALSE, 14 | keep_par_name = T 15 | ) 16 | } 17 | \arguments{ 18 | \item{bpc_object}{a bpc object} 19 | 20 | \item{params}{a vector with the parameters to use. If null then all will be present} 21 | 22 | \item{HPDI}{should return the HPDI or the credible intervals. Default is returning the HPDI} 23 | 24 | \item{credMass}{probability mass for the summary stats} 25 | 26 | \item{n_eff}{Should include the number of effective samples in the df} 27 | 28 | \item{Rhat}{Should include the Rhat in the df} 29 | 30 | \item{keep_par_name}{keep the parameter name e.g. lambda Graff instead of Graff. Default to T. Only valid for lambda, so we can have better ranks} 31 | } 32 | \description{ 33 | Function to retrieve a data frame with summary of the parameters 34 | } 35 | \examples{ 36 | \donttest{ 37 | m<-bpc(data = tennis_agresti, 38 | player0 = 'player0', 39 | player1 = 'player1', 40 | result_column = 'y', 41 | model_type = 'bt', 42 | solve_ties = 'none') 43 | param<-get_parameters_df(m) 44 | print(param) 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /man/get_parameters_plot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_plot.R 3 | \name{get_parameters_plot} 4 | \alias{get_parameters_plot} 5 | \title{Return a plot for the parameters estimates based on the HPD interval 6 | The returned plot is a caterpillar type of plot} 7 | \usage{ 8 | get_parameters_plot( 9 | bpc_object, 10 | HPDI = T, 11 | params = c("lambda"), 12 | title = "Parameter estimates", 13 | subtitle = NULL, 14 | xaxis = "Parameter", 15 | yaxis = "Value", 16 | rotate_x_labels = FALSE, 17 | APA = TRUE, 18 | keep_par_name = TRUE 19 | ) 20 | } 21 | \arguments{ 22 | \item{bpc_object}{a bpc fitted object} 23 | 24 | \item{HPDI}{use HPD (TRUE) or credible intervals (FALSE) for the plots} 25 | 26 | \item{params}{a vector of string for of the parameters to be plotted} 27 | 28 | \item{title}{the title of the plot} 29 | 30 | \item{subtitle}{optional subtitle for the plot} 31 | 32 | \item{xaxis}{title of the x axis} 33 | 34 | \item{yaxis}{title of the y axis} 35 | 36 | \item{rotate_x_labels}{should the labels be shown horizontally (default, FALSE) or vertically (TRUE)} 37 | 38 | \item{APA}{should the graphic be formatted in APA style (default TRUE)} 39 | 40 | \item{keep_par_name}{keep the parameter name e.g. lambda Graff instead of Graff. Default to T. Only valid for lambda, so we can have better ranks} 41 | } 42 | \value{ 43 | a ggplot2 caterpillar plot 44 | } 45 | \description{ 46 | Return a plot for the parameters estimates based on the HPD interval 47 | The returned plot is a caterpillar type of plot 48 | } 49 | \examples{ 50 | \donttest{ 51 | m<-bpc(data = tennis_agresti, 52 | player0 = 'player0', 53 | player1 = 'player1', 54 | result_column = 'y', 55 | model_type = 'bt', 56 | solve_ties = 'none') 57 | p<-get_parameters_plot(m) 58 | p 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /man/get_parameters_posterior.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_get_parameters.R 3 | \name{get_parameters_posterior} 4 | \alias{get_parameters_posterior} 5 | \title{Return a dataframe with the posterior distribution of each parameter used in the model} 6 | \usage{ 7 | get_parameters_posterior(bpc_object, n = 100) 8 | } 9 | \arguments{ 10 | \item{bpc_object}{a bpc object} 11 | 12 | \item{n}{number of samples in the posterior} 13 | } 14 | \value{ 15 | a dataframe 16 | } 17 | \description{ 18 | Return a dataframe with the posterior distribution of each parameter used in the model 19 | } 20 | \examples{ 21 | \donttest{ 22 | m<-bpc(data = tennis_agresti, 23 | player0 = 'player0', 24 | player1 = 'player1', 25 | result_column = 'y', 26 | model_type = 'bt', 27 | solve_ties = 'none') 28 | param<-get_parameters_posterior(m) 29 | print(head(param)) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /man/get_parameters_table.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_get_parameters.R 3 | \name{get_parameters_table} 4 | \alias{get_parameters_table} 5 | \title{Publication-ready table for the parameter estimates} 6 | \usage{ 7 | get_parameters_table( 8 | bpc_object, 9 | params = NULL, 10 | credMass = 0.95, 11 | format = "latex", 12 | digits = 3, 13 | caption = "Parameters estimates", 14 | HPDI = T, 15 | n_eff = F, 16 | keep_par_name = T 17 | ) 18 | } 19 | \arguments{ 20 | \item{bpc_object}{a bpc object} 21 | 22 | \item{params}{a vector with the parameters to be in the table. If NULL them all will be present} 23 | 24 | \item{credMass}{the probability mass for the credible interval} 25 | 26 | \item{format}{A character string. same formats utilized in the knitr::kable function 27 | \itemize{ 28 | \item 'latex': output in latex format 29 | \item 'simple': appropriated for the console 30 | \item 'pipe': Pandoc's pipe tables 31 | \item 'html': for html formats 32 | \item 'rst' 33 | }} 34 | 35 | \item{digits}{number of digits in the table} 36 | 37 | \item{caption}{a string containing the caption of the table} 38 | 39 | \item{HPDI}{a boolean if the intervals should be credible (F) or HPD intervals (T)} 40 | 41 | \item{n_eff}{a boolean. Should the number of effective samples be presented (T) or not (F default).} 42 | 43 | \item{keep_par_name}{keep the parameter name e.g. lambda Graff instead of Graff. Default to T. Only valid for lambda, so we can have better ranks} 44 | } 45 | \value{ 46 | a formatted table 47 | } 48 | \description{ 49 | Publication-ready table for the parameter estimates 50 | } 51 | \examples{ 52 | \donttest{ 53 | m<-bpc(data = tennis_agresti, 54 | player0 = 'player0', 55 | player1 = 'player1', 56 | result_column = 'y', 57 | model_type = 'bt', 58 | solve_ties = 'none') 59 | t<-get_parameters_table(m) 60 | print(t) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /man/get_probabilities.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_get_probabilities.R 3 | \name{get_probabilities} 4 | \alias{get_probabilities} 5 | \title{Get the empirical win/draw probabilities based on the ability/strength parameters.} 6 | \usage{ 7 | get_probabilities(bpc_object, newdata = NULL, n = 1000, model_type = NULL) 8 | } 9 | \arguments{ 10 | \item{bpc_object}{a bpc object} 11 | 12 | \item{newdata}{default to NULL. If used, it will calculate the probabilities only for the newdata. Otherwise it will calculate for all combinations} 13 | 14 | \item{n}{number of samples to draw from the posterior} 15 | 16 | \item{model_type}{when dealing with some models (such as random effects) one might want to make predictions using the estimated parameters with the random effects but without specifying the specific values of random effects to predict. Therefore one can set a subset of the model to make predictions. For example: a model sampled with bt-U can be used to make predictions of the model bt only.} 17 | } 18 | \value{ 19 | a list with data frame table with the respective probabilities and a matrix with the corresponding posterior 20 | } 21 | \description{ 22 | Get the empirical win/draw probabilities based on the ability/strength parameters. 23 | } 24 | -------------------------------------------------------------------------------- /man/get_probabilities_df.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_get_probabilities.R 3 | \name{get_probabilities_df} 4 | \alias{get_probabilities_df} 5 | \title{Get the empirical win/draw probabilities based on the ability/strength parameters.} 6 | \usage{ 7 | get_probabilities_df(bpc_object, newdata = NULL, n = 100, model_type = NULL) 8 | } 9 | \arguments{ 10 | \item{bpc_object}{a bpc object} 11 | 12 | \item{newdata}{default to NULL. If used, it will calculate the probabilities only for the newdata. Otherwise it will calculate for all combinations} 13 | 14 | \item{n}{number of samples to draw from the posterior} 15 | 16 | \item{model_type}{when dealing with some models (such as random effects) one might want to make predictions using the estimated parameters with the random effects but without specifying the specific values of random effects to predict. Therefore one can set a subset of the model to make predictions. For example: a model sampled with bt-U can be used to make predictions of the model bt only.} 17 | } 18 | \value{ 19 | a data frame table with the respective probabilities 20 | } 21 | \description{ 22 | Get the empirical win/draw probabilities based on the ability/strength parameters. 23 | } 24 | \examples{ 25 | \donttest{ 26 | m<-bpc(data = tennis_agresti, 27 | player0 = 'player0', 28 | player1 = 'player1', 29 | result_column = 'y', 30 | model_type = 'bt', 31 | solve_ties = 'none') 32 | prob<-get_probabilities_df(m) 33 | print(prob) 34 | 35 | # Now we can try just specifying a few data combinations of probabilities 36 | m2<-bpc(data = tennis_agresti, 37 | player0 = 'player0', 38 | player1 = 'player1', 39 | result_column = 'y', 40 | model_type = 'bt', 41 | solve_ties = 'none') 42 | prob<-get_probabilities_df(m2, newdata=tennis_agresti[c(1,15),]) 43 | print(prob) 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /man/get_probabilities_posterior.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_get_probabilities.R 3 | \name{get_probabilities_posterior} 4 | \alias{get_probabilities_posterior} 5 | \title{Get the posterior of the probabilities} 6 | \usage{ 7 | get_probabilities_posterior( 8 | bpc_object, 9 | newdata = NULL, 10 | n = 1000, 11 | model_type = NULL 12 | ) 13 | } 14 | \arguments{ 15 | \item{bpc_object}{a bpc object} 16 | 17 | \item{newdata}{default to NULL. If used, it will calculate the probabilities only for the newdata. Otherwise it will calculate for all combinations} 18 | 19 | \item{n}{number of samples to draw from the posterior} 20 | 21 | \item{model_type}{when dealing with some models (such as random effects) one might want to make predictions using the estimated parameters with the random effects but without specifying the specific values of random effects to predict. Therefore one can set a subset of the model to make predictions. For example: a model sampled with bt-U can be used to make predictions of the model bt only.} 22 | } 23 | \value{ 24 | a matrix with the corresponding posterior 25 | } 26 | \description{ 27 | Get the posterior of the probabilities 28 | } 29 | \examples{ 30 | \donttest{ 31 | m<-bpc(data = tennis_agresti, 32 | player0 = 'player0', 33 | player1 = 'player1', 34 | result_column = 'y', 35 | model_type = 'bt', 36 | solve_ties = 'none') 37 | prob<-get_probabilities_posterior(m) 38 | print(prob) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /man/get_probabilities_table.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_get_probabilities.R 3 | \name{get_probabilities_table} 4 | \alias{get_probabilities_table} 5 | \title{Publication-ready table for the probabilities} 6 | \usage{ 7 | get_probabilities_table( 8 | bpc_object, 9 | newdata = NULL, 10 | n = 100, 11 | format = "latex", 12 | digits = 3, 13 | caption = "Estimated posterior probabilites", 14 | model_type = NULL 15 | ) 16 | } 17 | \arguments{ 18 | \item{bpc_object}{a bpc object} 19 | 20 | \item{newdata}{default to NULL. If used, it will calculate the probabilities only for the newdata. Otherwise it will calculate for all combinations} 21 | 22 | \item{n}{Number of times to sample from the posterior} 23 | 24 | \item{format}{A character string. same formats utilized in the knitr::kable function 25 | \itemize{ 26 | \item 'latex': output in latex format 27 | \item 'simple': appropriated for the console 28 | \item 'pipe': Pandoc's pipe tables 29 | \item 'html': for html formats 30 | \item 'rst' 31 | Possible values are latex, html, p, simple (Pandoc's simple tables), and rst. 32 | }} 33 | 34 | \item{digits}{number of digits in the table} 35 | 36 | \item{caption}{a string containing the caption of the table} 37 | 38 | \item{model_type}{when dealing with some models (such as random effects) one might want to make predictions using the estimated parameters with the random effects but without specifying the specific values of random effects to predict. Therefore one can set a subset of the model to make predictions. For example: a model sampled with bt-U can be used to make predictions of the model bt only.} 39 | } 40 | \value{ 41 | a formatted table 42 | } 43 | \description{ 44 | Publication-ready table for the probabilities 45 | } 46 | \examples{ 47 | \donttest{ 48 | m<-bpc(data = tennis_agresti, 49 | player0 = 'player0', 50 | player1 = 'player1', 51 | result_column = 'y', 52 | model_type = 'bt', 53 | solve_ties = 'none') 54 | t<-get_probabilities_table(m) 55 | print(t) 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /man/get_rank_of_players.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_rank_of_players.R 3 | \name{get_rank_of_players} 4 | \alias{get_rank_of_players} 5 | \title{Generate a ranking of the ability based on sampling the posterior distribution of the ranks. 6 | This is not exported. Use either the get_rank_of_players_df or the get_rank_of_players_posterior functions} 7 | \usage{ 8 | get_rank_of_players(bpc_object, n = 1000) 9 | } 10 | \arguments{ 11 | \item{bpc_object}{a bpc object} 12 | 13 | \item{n}{Number of times we will sample the posterior} 14 | } 15 | \value{ 16 | a list containing the data frame that represents the table and a matrix containing the posterior distribution of the ranks 17 | } 18 | \description{ 19 | Generate a ranking of the ability based on sampling the posterior distribution of the ranks. 20 | This is not exported. Use either the get_rank_of_players_df or the get_rank_of_players_posterior functions 21 | } 22 | -------------------------------------------------------------------------------- /man/get_rank_of_players_df.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_rank_of_players.R 3 | \name{get_rank_of_players_df} 4 | \alias{get_rank_of_players_df} 5 | \title{Generate a ranking of the ability based on sampling the posterior distribution of the ranks.} 6 | \usage{ 7 | get_rank_of_players_df(bpc_object, n = 1000) 8 | } 9 | \arguments{ 10 | \item{bpc_object}{a bpc object} 11 | 12 | \item{n}{Number of times we will sample the posterior} 13 | } 14 | \value{ 15 | a data frame. This data frame contains the median of the rank, the mean, the standard deviation of the rank 16 | } 17 | \description{ 18 | To print this object you should remove the last column PosteriorRank since it contain the whole posterior distribution for each case 19 | } 20 | \examples{ 21 | \donttest{ 22 | m<-bpc(data = tennis_agresti, 23 | player0 = 'player0', 24 | player1 = 'player1', 25 | result_column = 'y', 26 | model_type = 'bt', 27 | solve_ties = 'none') 28 | rank_m<-get_rank_of_players_df(m,n=100) 29 | print(rank_m) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /man/get_rank_of_players_posterior.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_rank_of_players.R 3 | \name{get_rank_of_players_posterior} 4 | \alias{get_rank_of_players_posterior} 5 | \title{Generate a ranking of the ability based on sampling the posterior distribution of the ranks.} 6 | \usage{ 7 | get_rank_of_players_posterior(bpc_object, n = 1000) 8 | } 9 | \arguments{ 10 | \item{bpc_object}{a bpc object} 11 | 12 | \item{n}{Number of times we will sample the posterior} 13 | } 14 | \value{ 15 | a matrix containing the posterior distribution of the ranks 16 | } 17 | \description{ 18 | To print this object you should remove the last column PosteriorRank since it contain the whole posterior distribution for each case 19 | } 20 | \examples{ 21 | \donttest{ 22 | m<-bpc(data = tennis_agresti, 23 | player0 = 'player0', 24 | player1 = 'player1', 25 | result_column = 'y', 26 | model_type = 'bt', 27 | solve_ties = 'none') 28 | rank_m<-get_rank_of_players_posterior(m,n=100) 29 | print(rank_m) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /man/get_rank_of_players_table.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_rank_of_players.R 3 | \name{get_rank_of_players_table} 4 | \alias{get_rank_of_players_table} 5 | \title{Publication-ready table for the rank table} 6 | \usage{ 7 | get_rank_of_players_table( 8 | bpc_object, 9 | format = "latex", 10 | digits = 3, 11 | caption = "Estimated posterior ranks", 12 | n = 1000 13 | ) 14 | } 15 | \arguments{ 16 | \item{bpc_object}{a bpc object} 17 | 18 | \item{format}{A character string. same formats utilized in the knitr::kable function 19 | \itemize{ 20 | \item 'latex': output in latex format 21 | \item 'simple': appropriated for the console 22 | \item 'pipe': Pandoc's pipe tables 23 | \item 'html': for html formats 24 | \item 'rst' 25 | \item 'simple' appropriated for the console 26 | }} 27 | 28 | \item{digits}{number of digits in the table} 29 | 30 | \item{caption}{a string containing the caption of the table} 31 | 32 | \item{n}{number of times we are sampling the posterior} 33 | } 34 | \value{ 35 | a formatted table 36 | } 37 | \description{ 38 | Publication-ready table for the rank table 39 | } 40 | \examples{ 41 | \donttest{ 42 | m<-bpc(data = tennis_agresti, 43 | player0 = 'player0', 44 | player1 = 'player1', 45 | result_column = 'y', 46 | model_type = 'bt', 47 | solve_ties = 'none') 48 | t<-get_rank_of_players_table(m) 49 | print(t) 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /man/get_sample_posterior.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_exports.R 3 | \name{get_sample_posterior} 4 | \alias{get_sample_posterior} 5 | \title{Get the posterior samples for a parameter of the model.} 6 | \usage{ 7 | get_sample_posterior(bpc_object, par = "lambda", n = 1000, keep_par_name = T) 8 | } 9 | \arguments{ 10 | \item{bpc_object}{a bpc object} 11 | 12 | \item{par}{name of the parameters to predict} 13 | 14 | \item{n}{how many times are we sampling? Default 1000} 15 | 16 | \item{keep_par_name}{keep the parameter name e.g. lambda Graff instead of Graff. Default to T. Only valid for lambda, so we can have better ranks} 17 | } 18 | \value{ 19 | Return a data frame with the posterior samples for the parameters. One column for each parameter one row for each sample 20 | } 21 | \description{ 22 | Return a data frame with the posterior samples for the parameters of the model 23 | } 24 | \examples{ 25 | \donttest{ 26 | m<-bpc(data = tennis_agresti, 27 | player0 = 'player0', 28 | player1 = 'player1', 29 | result_column = 'y', 30 | model_type = 'bt', 31 | solve_ties = 'none') 32 | s <- get_sample_posterior(m, par='lambda', n=100) 33 | print(head(s)) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /man/get_waic.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_exports.R 3 | \name{get_waic} 4 | \alias{get_waic} 5 | \title{Tiny wrapper for the WAIC method from the loo package.} 6 | \usage{ 7 | get_waic(bpc_object) 8 | } 9 | \arguments{ 10 | \item{bpc_object}{a bpc object} 11 | } 12 | \value{ 13 | a loo object 14 | } 15 | \description{ 16 | This is used to evaluate the fit of the model using the Watanabe-Akaike Information criteria 17 | } 18 | \examples{ 19 | \donttest{ 20 | m<-bpc(data = tennis_agresti, 21 | player0 = 'player0', 22 | player1 = 'player1', 23 | result_column = 'y', 24 | model_type = 'bt', 25 | solve_ties = 'none') 26 | waic<-get_waic(m) 27 | print(waic) 28 | } 29 | } 30 | \references{ 31 | Gelman, Andrew, Jessica Hwang, and Aki Vehtari. Understanding predictive information criteria for Bayesian models. Statistics and computing 24.6 (2014): 997-1016. 32 | } 33 | -------------------------------------------------------------------------------- /man/inv_logit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_exports.R 3 | \name{inv_logit} 4 | \alias{inv_logit} 5 | \title{Inverse logit function} 6 | \usage{ 7 | inv_logit(x) 8 | } 9 | \arguments{ 10 | \item{x}{is a real -inf to inf} 11 | } 12 | \value{ 13 | a value between 0 and 1 14 | } 15 | \description{ 16 | Inverse logit function 17 | } 18 | \examples{ 19 | inv_logit(5) 20 | inv_logit(-5) 21 | inv_logit(0) 22 | } 23 | \references{ 24 | https://en.wikipedia.org/wiki/Logit 25 | } 26 | -------------------------------------------------------------------------------- /man/launch_shinystan.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_exports.R 3 | \name{launch_shinystan} 4 | \alias{launch_shinystan} 5 | \title{Tiny wrapper to launch a shinystan app to investigate the MCMC. 6 | It launches a shinystan app automatically in the web browser 7 | This function requires having rstan and shinystan installed} 8 | \usage{ 9 | launch_shinystan(bpc_object) 10 | } 11 | \arguments{ 12 | \item{bpc_object}{a bpc object} 13 | } 14 | \description{ 15 | Tiny wrapper to launch a shinystan app to investigate the MCMC. 16 | It launches a shinystan app automatically in the web browser 17 | This function requires having rstan and shinystan installed 18 | } 19 | \examples{ 20 | \dontrun{ 21 | m<-bpc(data = tennis_agresti, 22 | player0 = 'player0', 23 | player1 = 'player1', 24 | result_column = 'y', 25 | model_type = 'bt', 26 | solve_ties = 'none') 27 | launch_shinystan(m) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /man/load_bpc_model.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_exports.R 3 | \name{load_bpc_model} 4 | \alias{load_bpc_model} 5 | \title{Thin wrapper to load a saved bpc model for examining later} 6 | \usage{ 7 | load_bpc_model(file_name_with_path) 8 | } 9 | \arguments{ 10 | \item{file_name_with_path}{the file name with path following the conventions of the operating system that points to a saved bpc object including the RDS} 11 | } 12 | \description{ 13 | Thin wrapper to load a saved bpc model for examining later 14 | } 15 | -------------------------------------------------------------------------------- /man/logit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_exports.R 3 | \name{logit} 4 | \alias{logit} 5 | \title{Logit function} 6 | \usage{ 7 | logit(x) 8 | } 9 | \arguments{ 10 | \item{x}{p is a probability 0 to 1} 11 | } 12 | \value{ 13 | a value between -inf and inf 14 | } 15 | \description{ 16 | Logit function 17 | } 18 | \examples{ 19 | logit(0.5) 20 | logit(0.2) 21 | } 22 | \references{ 23 | https://en.wikipedia.org/wiki/Logit 24 | } 25 | -------------------------------------------------------------------------------- /man/match_cluster_names_to_cluster_lookup_table.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_indexes.R 3 | \name{match_cluster_names_to_cluster_lookup_table} 4 | \alias{match_cluster_names_to_cluster_lookup_table} 5 | \title{Receives a column with cluster names and returns a data frame with the relevant index column based on a given cluster lookup table} 6 | \usage{ 7 | match_cluster_names_to_cluster_lookup_table( 8 | d, 9 | cluster, 10 | cluster_lookup_table, 11 | i 12 | ) 13 | } 14 | \arguments{ 15 | \item{d}{a data frame} 16 | 17 | \item{cluster}{The name of the column of data data contains player0} 18 | 19 | \item{cluster_lookup_table}{a lookup table for the cluster} 20 | 21 | \item{i}{number of the cluster} 22 | } 23 | \value{ 24 | A dataframe with the additional columns 'cluster_index' that contains the indexes 25 | } 26 | \description{ 27 | Receives a column with cluster names and returns a data frame with the relevant index column based on a given cluster lookup table 28 | } 29 | -------------------------------------------------------------------------------- /man/match_player_names_to_lookup_table.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_indexes.R 3 | \name{match_player_names_to_lookup_table} 4 | \alias{match_player_names_to_lookup_table} 5 | \title{Receives two columns with player names and returns a data frame with the relevant index columns based on a given lookup table} 6 | \usage{ 7 | match_player_names_to_lookup_table(d, player0, player1, lookup_table) 8 | } 9 | \arguments{ 10 | \item{d}{a data frame} 11 | 12 | \item{player0}{The name of the column of data data contains player0} 13 | 14 | \item{player1}{The name of the column of data data contains player1} 15 | 16 | \item{lookup_table}{a lookup table data frame} 17 | } 18 | \value{ 19 | A dataframe with the additional columns 'player0_index' and 'player1_index' that contains the indexes 20 | } 21 | \description{ 22 | Receives two columns with player names and returns a data frame with the relevant index columns based on a given lookup table 23 | } 24 | -------------------------------------------------------------------------------- /man/optimization_algorithms.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{optimization_algorithms} 5 | \alias{optimization_algorithms} 6 | \title{Dataset containing an example of the performance of different optimization algorithms against different benchmark functions. 7 | This is a reduced version of the dataset presented at the paper: "Statistical Models for the Analysis of Optimization Algorithms with Benchmark Functions.". 8 | For details on how the data was collected we refer to the paper.} 9 | \format{ 10 | This is the expansion of the data where each row contains 1 match only 11 | \itemize{ 12 | \item Algorithm: name of algorithm 13 | \item Benchmark: name of the benchmark problem 14 | \item TrueRewardDifference: Difference between the minimum function value obtained by the algorithm and the known global minimum 15 | \item Ndimensions: Number of dimensions of the benchmark problem 16 | \item MaxFevalPerDimensions: Maximum allowed budget for the algorithm per dimensions of the benchmark problem 17 | \item simNumber: id of the simulation. Indicates the repeated measures of each algorithm in each benchmark 18 | } 19 | } 20 | \source{ 21 | Mattos, David Issa, Jan Bosch, and Helena Holmstrom Olsson. Statistical Models for the Analysis of Optimization Algorithms with Benchmark Functions. arXiv preprint arXiv:2010.03783 (2020). 22 | } 23 | \usage{ 24 | optimization_algorithms 25 | } 26 | \description{ 27 | Dataset containing an example of the performance of different optimization algorithms against different benchmark functions. 28 | This is a reduced version of the dataset presented at the paper: "Statistical Models for the Analysis of Optimization Algorithms with Benchmark Functions.". 29 | For details on how the data was collected we refer to the paper. 30 | } 31 | \keyword{data} 32 | -------------------------------------------------------------------------------- /man/pipe.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils-pipe.R 3 | \name{\%>\%} 4 | \alias{\%>\%} 5 | \title{Pipe operator} 6 | \usage{ 7 | lhs \%>\% rhs 8 | } 9 | \description{ 10 | See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. 11 | } 12 | \keyword{internal} 13 | -------------------------------------------------------------------------------- /man/plot.bpc.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_s3.R 3 | \name{plot.bpc} 4 | \alias{plot.bpc} 5 | \title{S3 plot function for the parameter plot of a bpc model 6 | This is just a wrapper for the get_parameters_plot function and can be used interchangebly} 7 | \usage{ 8 | \method{plot}{bpc}( 9 | x, 10 | y = NULL, 11 | HPDI = T, 12 | params = c("lambda"), 13 | title = "Strength estimates", 14 | subtitle = NULL, 15 | xaxis = "Player", 16 | yaxis = "Value", 17 | rotate_x_labels = FALSE, 18 | APA = TRUE, 19 | keep_par_name = FALSE, 20 | ... 21 | ) 22 | } 23 | \arguments{ 24 | \item{x}{a bpc object} 25 | 26 | \item{y}{Not used. Default to NULL} 27 | 28 | \item{HPDI}{use HPD (TRUE) or credible intervals (FALSE) for the plots} 29 | 30 | \item{params}{a vector of string for of the parameters to be plotted} 31 | 32 | \item{title}{the title of the plot} 33 | 34 | \item{subtitle}{optional subtitle for the plot} 35 | 36 | \item{xaxis}{title of the x axis} 37 | 38 | \item{yaxis}{title of the y axis} 39 | 40 | \item{rotate_x_labels}{should the labels be shown horizontally (default, FALSE) or vertically (TRUE)} 41 | 42 | \item{APA}{should the graphic be formatted in APA style (default TRUE)} 43 | 44 | \item{keep_par_name}{keep the parameter name e.g. lambda Graff instead of Graff. Default to T. Only valid for lambda, so we can have better ranks} 45 | 46 | \item{...}{additional parameters for the generic S3 plot function. Not used.} 47 | } 48 | \value{ 49 | a ggplot2 caterpillar plot 50 | } 51 | \description{ 52 | S3 plot function for the parameter plot of a bpc model 53 | This is just a wrapper for the get_parameters_plot function and can be used interchangebly 54 | } 55 | \examples{ 56 | \donttest{ 57 | m<-bpc(data = tennis_agresti, 58 | player0 = 'player0', 59 | player1 = 'player1', 60 | result_column = 'y', 61 | model_type = 'bt', 62 | solve_ties = 'none') 63 | p<-get_parameters_plot(m) 64 | p 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /man/posterior_predictive.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_exports.R 3 | \name{posterior_predictive} 4 | \alias{posterior_predictive} 5 | \title{Calculate the posterior predictive distributions. 6 | Helps to check the fitness of the model. Use it in conjunction with the shinystan app to visualize some nice posterior predictive plots} 7 | \usage{ 8 | posterior_predictive(bpc_object, n = 100) 9 | } 10 | \arguments{ 11 | \item{bpc_object}{a bpc object} 12 | 13 | \item{n}{number of times to sample from the posterior} 14 | } 15 | \value{ 16 | a list containing two values: y value that represents the collected data and y_pred that represents the predictive posterior matrix 17 | } 18 | \description{ 19 | Calculate the posterior predictive distributions. 20 | Helps to check the fitness of the model. Use it in conjunction with the shinystan app to visualize some nice posterior predictive plots 21 | } 22 | \examples{ 23 | \donttest{ 24 | m<-bpc(data = tennis_agresti, 25 | player0 = 'player0', 26 | player1 = 'player1', 27 | result_column = 'y', 28 | model_type = 'bt', 29 | solve_ties = 'none') 30 | pp<-posterior_predictive(m) 31 | print(pp$y) 32 | print(pp$y_pred) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /man/predict.bpc.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_s3.R 3 | \name{predict.bpc} 4 | \alias{predict.bpc} 5 | \title{Predict results for new data.} 6 | \usage{ 7 | \method{predict}{bpc}( 8 | object, 9 | newdata, 10 | predictors = NULL, 11 | n = 100, 12 | return_matrix = T, 13 | model_type = NULL, 14 | ... 15 | ) 16 | } 17 | \arguments{ 18 | \item{object}{a bpc object} 19 | 20 | \item{newdata}{a data frame that contains columns with the same names as used to fit the data in the model.} 21 | 22 | \item{predictors}{A data frame that contains the players predictors values when using a generalized model. Should be set only if using the generalized models. Only numeric values are accepted. Booleans are accepted but will be cast into integers. The first column should be for the player name, the others will be the predictors. The column names will be used as name for the predictors} 23 | 24 | \item{n}{number of time we will iterate and get the posterior. default is 100 so we dont get too many} 25 | 26 | \item{return_matrix}{should we return only a matrix with the predictive values. Default F. Use this to combine with predictive posterior plots in bayesplot} 27 | 28 | \item{model_type}{when dealing with some models (such as random effects) one might want to make predictions using the estimated parameters with the random effects but without specifying the specific values of random effects to predict. Therefore one can set a subset of the model to make predictions. For example: a model sampled with bt-U can be used to make predictions of the model bt only.} 29 | 30 | \item{\dots}{additional parameters for the generic predict function. Not used.} 31 | } 32 | \value{ 33 | a dataframe or a matrix depending on the return_matrix parameter 34 | } 35 | \description{ 36 | This S3 function receives the bpc model and a data frame containing the same columns as the one used to fit the model. 37 | It returns another data frame with with the same columns of the new data and n additional columns representing a posterior preditive distribution. 38 | See the vignettes for a larger examples with the usage of this function 39 | } 40 | \examples{ 41 | \donttest{ 42 | m<-bpc(data = tennis_agresti, 43 | player0 = 'player0', 44 | player1 = 'player1', 45 | result_column = 'y', 46 | model_type = 'bt', 47 | solve_ties = 'none') 48 | predict(m,newdata=tennis_agresti) 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /man/print.bpc.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_s3.R 3 | \name{print.bpc} 4 | \alias{print.bpc} 5 | \title{Print method for the bpc object.} 6 | \usage{ 7 | \method{print}{bpc}(x, credMass = 0.95, HPDI = T, digits = 3, diagnostics = TRUE, ...) 8 | } 9 | \arguments{ 10 | \item{x}{a bpc object} 11 | 12 | \item{credMass}{the probability mass for the credible interval} 13 | 14 | \item{HPDI}{True if show HPDI interval, False to show the credible (quantile) intervals} 15 | 16 | \item{digits}{number of decimal digits in the table} 17 | 18 | \item{diagnostics}{show diagnostics} 19 | 20 | \item{\dots}{additional parameters for the generic print function. Not used} 21 | } 22 | \description{ 23 | This S3 functions only prints the mean and the HDPI values of all the parameters in the model 24 | } 25 | \examples{ 26 | \donttest{ 27 | m<-bpc(data = tennis_agresti, 28 | player0 = 'player0', 29 | player1 = 'player1', 30 | result_column = 'y', 31 | model_type = 'bt', 32 | solve_ties = 'none') 33 | print(m) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /man/replace_parameter_index_with_names.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_indexes.R 3 | \name{replace_parameter_index_with_names} 4 | \alias{replace_parameter_index_with_names} 5 | \title{Replace the name of the parameter from index to name using a lookup_table 6 | Receives a data frame and returns a dataframe.} 7 | \usage{ 8 | replace_parameter_index_with_names( 9 | d, 10 | column, 11 | par, 12 | lookup_table, 13 | cluster_lookup_table = NULL, 14 | predictors_lookup_table = NULL, 15 | subject_predictors_lookup_table = NULL, 16 | keep_par_name = T 17 | ) 18 | } 19 | \arguments{ 20 | \item{d}{dataframe} 21 | 22 | \item{column}{name of the colum} 23 | 24 | \item{par}{name of the parameter} 25 | 26 | \item{lookup_table}{lookup table of the players} 27 | 28 | \item{cluster_lookup_table}{a lookup table of the predictors} 29 | 30 | \item{predictors_lookup_table}{a lookup table for the predictors} 31 | 32 | \item{subject_predictors_lookup_table}{a lookup table for the subject predictors} 33 | 34 | \item{keep_par_name}{keep the parameter name e.g. lambda Graff instead of Graff. Default to T. Only valid for lambda, so we can have better ranks} 35 | } 36 | \value{ 37 | a data. frame where we change the names in the variable colum to the corresponding parameter_name from the lookup table 38 | } 39 | \description{ 40 | Replace the name of the parameter from index to name using a lookup_table 41 | Receives a data frame and returns a dataframe. 42 | } 43 | -------------------------------------------------------------------------------- /man/sample_stanfit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers.R 3 | \name{sample_stanfit} 4 | \alias{sample_stanfit} 5 | \title{Return a data frame by resampling the posterior from a stanfit 6 | Here we select a parameter, retrieve the all the posterior from the stanfit and then we resample this posterior n times} 7 | \usage{ 8 | sample_stanfit(stanfit, par, n = 100) 9 | } 10 | \arguments{ 11 | \item{stanfit}{stanfit object} 12 | 13 | \item{par}{parameter name} 14 | 15 | \item{n}{number of samples} 16 | } 17 | \value{ 18 | a dataframe containing the samples of the parameter. Each column is a parameter (in order of the index), each row is a sample 19 | } 20 | \description{ 21 | Return a data frame by resampling the posterior from a stanfit 22 | Here we select a parameter, retrieve the all the posterior from the stanfit and then we resample this posterior n times 23 | } 24 | \references{ 25 | Stan Development Team (2020). RStan: the R interface to Stan. R package version 2.21.2. http://mc-stan.org/. 26 | } 27 | -------------------------------------------------------------------------------- /man/save_bpc_model.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_exports.R 3 | \name{save_bpc_model} 4 | \alias{save_bpc_model} 5 | \title{Thin wrapper to save the bpc model for examining later} 6 | \usage{ 7 | save_bpc_model(bpc_object, filename, path = NULL) 8 | } 9 | \arguments{ 10 | \item{bpc_object}{a bpc object} 11 | 12 | \item{filename}{string with the file name} 13 | 14 | \item{path}{string with path following the conventions of the operating system. If not provided it will use the default folder of the csv files .bpcs in the current working directory} 15 | } 16 | \description{ 17 | Thin wrapper to save the bpc model for examining later 18 | } 19 | -------------------------------------------------------------------------------- /man/summary.bpc.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_s3.R 3 | \name{summary.bpc} 4 | \alias{summary.bpc} 5 | \title{Summary of the model bpc model.} 6 | \usage{ 7 | \method{summary}{bpc}( 8 | object, 9 | digits = 2, 10 | credMass = 0.95, 11 | HPDI = TRUE, 12 | show_probabilities = TRUE, 13 | ... 14 | ) 15 | } 16 | \arguments{ 17 | \item{object}{bpc object} 18 | 19 | \item{digits}{number of decimal digits in the table} 20 | 21 | \item{credMass}{the probability mass for the credible interval} 22 | 23 | \item{HPDI}{True if show HPDI interval, False to show the credible (quantile) intervals} 24 | 25 | \item{show_probabilities}{should the tables of probabilities (Table 2) be displayed or not. Default to T but it is recommended to turn to F if either it has a large number of players (15+) or a large number of players and cluster, the table grows combinatorially.} 26 | 27 | \item{\dots}{additional parameters for the generic summary function. Not used.} 28 | } 29 | \description{ 30 | \itemize{ 31 | \item Table 1: Contains the parameter estimates and respective HPD interval 32 | \item Table 2: Contains the posterior probability for the combination of all players 33 | \item Table 3: Contains the ranking of the players' abilities based on the posterior distribution of the ranks 34 | } 35 | } 36 | \examples{ 37 | \donttest{ 38 | m<-bpc(data = tennis_agresti, 39 | player0 = 'player0', 40 | player1 = 'player1', 41 | result_column = 'y', 42 | model_type = 'bt', 43 | solve_ties = 'none') 44 | summary(m) 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /man/summary_from_fit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bpc_helpers_hpd.R 3 | \name{summary_from_fit} 4 | \alias{summary_from_fit} 5 | \title{Summary for all parameters from a stanfit object 6 | Here we use the coda package} 7 | \usage{ 8 | summary_from_fit(fit, pars, credMass = 0.95) 9 | } 10 | \arguments{ 11 | \item{fit}{a cmdstanr fit object retrieved from a bpc object} 12 | 13 | \item{pars}{the model parameters} 14 | 15 | \item{credMass}{Credibility mass for the interval} 16 | } 17 | \value{ 18 | a data frame with the HPDI calculated from the coda package 19 | } 20 | \description{ 21 | Summary for all parameters from a stanfit object 22 | Here we use the coda package 23 | } 24 | \references{ 25 | Martyn Plummer, Nicky Best, Kate Cowles and Karen Vines (2006). CODA: Convergence Diagnosis and Output Analysis for MCMC, R News, vol 6, 7-11 26 | } 27 | -------------------------------------------------------------------------------- /man/tennis_agresti.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{tennis_agresti} 5 | \alias{tennis_agresti} 6 | \title{This is the expansion of the tennis data from Agresti (2003) p.449 7 | This data refers to matches for several women tennis players during 1989 and 1990} 8 | \format{ 9 | This is the expansion of the data where each row contains 1 match only 10 | \itemize{ 11 | \item player0: name of player0 12 | \item player1: name of player1 13 | \item y: corresponds to the result of the match: 0 if player0 won, 1 if player1 won. 14 | \item id: is a column to make each row unique in the data. It does not have any particular interpretation 15 | } 16 | } 17 | \source{ 18 | Agresti, Alan. Categorical data analysis. Vol. 482. John Wiley & Sons, 2003. 19 | } 20 | \usage{ 21 | tennis_agresti 22 | } 23 | \description{ 24 | This is the expansion of the tennis data from Agresti (2003) p.449 25 | This data refers to matches for several women tennis players during 1989 and 1990 26 | } 27 | \keyword{data} 28 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(bpcs) 3 | 4 | test_check("bpcs") 5 | -------------------------------------------------------------------------------- /tests/testthat/helper-functions.R: -------------------------------------------------------------------------------- 1 | load_testdata<-function(dataset){ 2 | dataset<-paste(dataset,".rda",sep = "") 3 | f<-system.file("testdata",dataset, package = "bpcs",mustWork = TRUE) 4 | out<-get(load(file=f)) 5 | return(out) 6 | } 7 | 8 | 9 | #save and restore previous seed locally 10 | 11 | get_rand_state <- function() { 12 | # Using `get0()` here to have `NULL` output in case object doesn't exist. 13 | # Also using `inherits = FALSE` to get value exactly from global environment 14 | # and not from one of its parent. 15 | get0(".Random.seed", envir = .GlobalEnv, inherits = FALSE) 16 | } 17 | set_rand_state <- function(state) { 18 | # Assigning `NULL` state might lead to unwanted consequences 19 | if (!is.null(state)) { 20 | assign(".Random.seed", state, envir = .GlobalEnv, inherits = FALSE) 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/testthat/helper-no_error.R: -------------------------------------------------------------------------------- 1 | expect_no_error <- function(object) { 2 | expect_error(object, NA) 3 | } 4 | -------------------------------------------------------------------------------- /tests/testthat/test-bpc.R: -------------------------------------------------------------------------------- 1 | test_that("Input errors are caught in bpc", { 2 | #missing either player scores and the result column 3 | #mising player0_score and result column 4 | test_btscores<-load_testdata("test_btscores") 5 | 6 | 7 | expect_error(bpc(data=test_btscores, 8 | player0 = 'player0', 9 | player1 = 'player1', 10 | player1_score = 'score1', 11 | model_type='bt', 12 | solve_ties='random', 13 | win_score = 'higher')) 14 | #mising player1_score and result column 15 | expect_error(bpc(data=test_btscores, 16 | player0 = 'player0', 17 | player1 = 'player1', 18 | player1_score = 'score0', 19 | model_type='bt', 20 | solve_ties='random', 21 | win_score = 'higher')) 22 | #missing both scores and result column 23 | expect_error(bpc(data=test_btscores, 24 | player0 = 'player0', 25 | player1 = 'player1', 26 | model_type='bt', 27 | solve_ties='random', 28 | win_score = 'higher')) 29 | 30 | # input is not data frame or tibble # weird input for example 31 | expect_error(bpc(data=c(1,1,2), 32 | player0 = 'player0', 33 | player1 = 'player1', 34 | results_column='y', 35 | model_type='bt', 36 | solve_ties='random', 37 | win_score = 'higher')) 38 | 39 | 40 | 41 | }) 42 | 43 | 44 | test_that("Input warnings are caught in bpc", { 45 | test_davidson<-load_testdata("test_davidson") 46 | expect_warning(bpc(data=test_davidson, 47 | player0 = 'player0', 48 | player1 = 'player1', 49 | result_column = 'y', 50 | model_type='davidson', 51 | solve_ties='random', 52 | win_score = 'higher', 53 | iter=1000, 54 | warmup=300, 55 | show_chain_messages=F)) 56 | }) 57 | 58 | test_that('if there are ties AND solte_ties is none AND model is not davidson gives error',{ 59 | test_davidson<-load_testdata("test_davidson") 60 | expect_error(bpc(data=test_davidson,#there are ties here 61 | player0 = 'player0', 62 | player1 = 'player1', 63 | results_column = 'y', 64 | model_type='bt', #model is not davidson 65 | solve_ties='none', #method is none 66 | win_score = 'higher')) 67 | }) 68 | 69 | 70 | test_that('if has z_player1 column but not the correct model gives error',{ 71 | test_btorder<-load_testdata("test_btorder") 72 | expect_error(bpc(data=test_btorder,#there are ties here 73 | player0 = 'player0', 74 | player1 = 'player1', 75 | result_column = 'y', 76 | z_player1 = 'y', 77 | model_type='bt', #model is not btordereffect 78 | solve_ties='none', 79 | win_score = 'higher')) 80 | }) 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /tests/testthat/test-bpc_data_transformation.R: -------------------------------------------------------------------------------- 1 | test_that("expand_aggregated_data works", { 2 | df1<- tibble::tribble(~player0, ~player1, ~wins0, ~wins1,~cluster, 3 | 'A','B',4, 3, 'c1') 4 | 5 | df1_result<- as.data.frame(tibble::tribble(~player0, ~player1, ~y,~cluster, ~rowid, 6 | 'A','B', 0, 'c1', 1, 7 | 'A','B', 0, 'c1', 2, 8 | 'A','B', 0, 'c1', 3, 9 | 'A','B', 0, 'c1', 4, 10 | 'A','B', 1, 'c1', 5, 11 | 'A','B', 1, 'c1', 6, 12 | 'A','B', 1, 'c1', 7)) 13 | 14 | df2<- as.data.frame(tibble::tribble(~player0, ~player1, ~wins0, ~wins1,~cluster1,~cluster2, 15 | 'A','B',4, 3, 'c1', 'a1', 16 | 'A','C',1, 2, 'c2', 'a2')) 17 | 18 | df2_result<- as.data.frame(tibble::tribble(~player0, ~player1, ~y,~cluster1, ~cluster2 ,~rowid, 19 | 'A','B', 0, 'c1','a1', 1, 20 | 'A','B', 0, 'c1','a1', 2, 21 | 'A','B', 0, 'c1','a1', 3, 22 | 'A','B', 0, 'c1','a1', 4, 23 | 'A','B', 1, 'c1','a1', 5, 24 | 'A','B', 1, 'c1','a1', 6, 25 | 'A','B', 1, 'c1','a1', 7, 26 | 'A','C', 0, 'c2','a2', 8, 27 | 'A','C', 1, 'c2','a2', 9, 28 | 'A','C', 1, 'c2','a2', 10)) 29 | 30 | df3<- tibble::tribble(~player0, ~player1, ~wins0, ~wins1, ~ties, ~cluster, 31 | 'A','B',4, 3, 2, 'c1') 32 | 33 | df3_result <- as.data.frame(tibble::tribble(~player0, ~player1, ~y, ~rowid, 34 | 'A','B', 0, 1, 35 | 'A','B', 0, 2, 36 | 'A','B', 0, 3, 37 | 'A','B', 0, 4, 38 | 'A','B', 1, 5, 39 | 'A','B', 1, 6, 40 | 'A','B', 1, 7, 41 | 'A','B', 2, 8, 42 | 'A','B', 2, 9,)) 43 | 44 | expect_equal(expand_aggregated_data(df1,'player0', 'player1', 'wins0', 'wins1', keep=c('cluster')), df1_result) 45 | expect_equal(expand_aggregated_data(df2,'player0', 'player1', 'wins0', 'wins1', keep=c('cluster1','cluster2')), df2_result) 46 | expect_equal(expand_aggregated_data(df3,'player0', 'player1', 'wins0', 'wins1', ties='ties', keep = NULL),df3_result) 47 | 48 | 49 | }) 50 | -------------------------------------------------------------------------------- /tests/testthat/test-bpc_exports.R: -------------------------------------------------------------------------------- 1 | test_that("get_sample_posterior works", { 2 | skip_on_cran() 3 | test_bt<-load_testdata("test_bt") 4 | m<-bpc(data=test_bt, 5 | player0 = 'player0', 6 | player1 = 'player1', 7 | result_column = 'y', 8 | model_type='bt', 9 | solve_ties='random', 10 | win_score = 'higher', 11 | show_chain_messages=F) 12 | post<- get_sample_posterior(m, par = 'lambda', n=1000) 13 | expect_equal(ncol(post), 3) 14 | expect_equal(nrow(post), 1000) 15 | expect_equal(colnames(post), c('lambda[A]', 'lambda[B]', 'lambda[C]')) 16 | }) 17 | 18 | 19 | 20 | test_that("Logit test if x is in bounds", { 21 | expect_error(logit(-1),'Error!') 22 | expect_error(logit(10),'Error!') 23 | }) 24 | 25 | test_that('Known logit values', { 26 | expect_equal(logit(0.5),0) 27 | expect_equal(logit(1),Inf) 28 | expect_equal(logit(0),-Inf) 29 | }) 30 | 31 | 32 | test_that('Known inv logit values', { 33 | expect_lt(inv_logit(5)-0.9933071, 0.001) 34 | expect_lt(inv_logit(-5)-0.006692851, 0.001) 35 | expect_lt(inv_logit(0)-0.5, 0.001) 36 | }) 37 | -------------------------------------------------------------------------------- /tests/testthat/test-bpc_helpers.R: -------------------------------------------------------------------------------- 1 | test_that("compute_score works with btscores dataset", { 2 | test_btscores<-load_testdata('test_btscores') 3 | test_btscores_higher <- as.data.frame(tibble::tribble(~y,0,0,1,0,0,0,0,0,1)) 4 | test_btscores_lower <- as.data.frame(tibble::tribble(~y,1,1,0,1,1,1,1,1,0)) 5 | 6 | 7 | expect_equal(compute_scores(test_btscores, player0_score='score0', player1_score='score1', solve_ties='none', win_score='higher')$y, test_btscores_higher$y) 8 | expect_equal(compute_scores(test_btscores, player0_score='score0', player1_score='score1', solve_ties='none', win_score='lower')$y, test_btscores_lower$y) 9 | 10 | }) 11 | 12 | test_that("compute_score works with davidsonscores dataset", { 13 | test_davidsonscores<-load_testdata('test_davidsonscores') 14 | test_davidsonscores_1 <- as.data.frame(tibble::tribble(~y,0,0,0,0,0,0,0,0,0))#0 win both ties 15 | test_davidsonscores_2 <- as.data.frame(tibble::tribble(~y,0,0,0,0,0,0,0,0,1))#0 wins first and 1 wins last 16 | test_davidsonscores_3 <- as.data.frame(tibble::tribble(~y,0,0,1,0,0,0,0,0,0))#1 wins first and 0 last 17 | test_davidsonscores_4 <- as.data.frame(tibble::tribble(~y,0,0,1,0,0,0,0,0,1))#1 wins both 18 | test_davidsonscores_5 <- as.data.frame(tibble::tribble(~y,0,0,0,0,0,0,0))#1 remove ties 19 | 20 | 21 | set.seed(2)#case 1 22 | expect_equal(compute_scores(test_davidsonscores, player0_score='score0', player1_score='score1',solve_ties='random', win_score='higher')$y, test_davidsonscores_1$y) 23 | 24 | set.seed(1)#case 2 25 | expect_equal(compute_scores(test_davidsonscores, player0_score='score0', player1_score='score1',solve_ties='random', win_score='higher')$y, test_davidsonscores_2$y) 26 | 27 | set.seed(4)#case 3 28 | expect_equal(compute_scores(test_davidsonscores, player0_score='score0', player1_score='score1',solve_ties='random', win_score='higher')$y, test_davidsonscores_3$y) 29 | 30 | set.seed(55)#case 4 31 | expect_equal(compute_scores(test_davidsonscores, player0_score='score0', player1_score='score1',solve_ties='random', win_score='higher')$y, test_davidsonscores_4$y) 32 | 33 | #case 5 34 | expect_equal(compute_scores(test_davidsonscores, player0_score='score0', player1_score='score1',solve_ties='remove', win_score='higher')$y, test_davidsonscores_5$y) 35 | 36 | 37 | }) 38 | 39 | 40 | test_that('compute_ties works',{ 41 | v1 <- data.frame(results=c(1,0,2,0,0,1)) 42 | v2 <- data.frame(results=c(1,1,2,2,0,1)) 43 | v3 <- data.frame(results=c(1,1,0,0,0,1)) 44 | rv1 <- data.frame(ties=c(0,0,1,0,0,0)) 45 | rv2 <- data.frame(ties=c(0,0,1,1,0,0)) 46 | rv3 <- data.frame(ties=c(0,0,0,0,0,0)) 47 | 48 | expect_equal(compute_ties(v1,'results')$ties, rv1$ties) 49 | expect_equal(compute_ties(v2,'results')$ties, rv2$ties) 50 | expect_equal(compute_ties(v3,'results')$ties, rv3$ties) 51 | }) 52 | 53 | 54 | test_that('fix_ties works',{ 55 | old_state <- get_rand_state() #saving seed state 56 | 57 | test_davidson<-load_testdata('test_davidson') 58 | 59 | v_none <- test_davidson 60 | 61 | #four possibilities 62 | v_random1 <- test_davidson 63 | v_random1$y <- c(0, 0, 0, 0, 0, 0, 0, 0, 0) 64 | v_random2 <- test_davidson 65 | v_random2$y <- c(0, 0, 1, 0, 0, 0, 0, 0, 0) 66 | v_random3 <- test_davidson 67 | v_random3$y <- c(0, 0, 0, 0, 0, 0, 0, 0, 1) 68 | v_random4 <- test_davidson 69 | v_random4$y <- c(0, 0, 1, 0, 0, 0, 0, 0, 1) 70 | 71 | v_remove <- test_davidson 72 | v_remove <- v_remove[-c(3,9),] 73 | row.names(v_remove) <- c() 74 | 75 | 76 | expect_equal(fix_ties(test_davidson,'none'), v_none) 77 | set.seed(33) 78 | expect_equal(fix_ties(test_davidson,'random'), v_random4) 79 | set.seed(353) 80 | expect_equal(fix_ties(test_davidson,'random'), v_random2) 81 | set.seed(3) 82 | expect_equal(fix_ties(test_davidson,'random'), v_random3) 83 | set.seed(42) 84 | expect_equal(fix_ties(test_davidson,'random'), v_random1) 85 | expect_equal(fix_ties(test_davidson,'remove'), v_remove) 86 | 87 | on.exit(set_rand_state(old_state)) 88 | }) 89 | 90 | 91 | test_that('calculate_prob_from_vector works',{ 92 | v1 <- c(1,0,2,0,0,1) 93 | v2 <- c(1,1,1,0,0,0) 94 | v3 <- c(1,1,1,1,1) 95 | v4 <- c(0,0,0,0) 96 | 97 | 98 | expect_equal(calculate_prob_from_vector(v1,1), 2/6) 99 | expect_equal(calculate_prob_from_vector(v1,0), 3/6) 100 | expect_equal(calculate_prob_from_vector(v1,2), 1/6) 101 | expect_equal(calculate_prob_from_vector(v2,1), 3/6) 102 | expect_equal(calculate_prob_from_vector(v2,0), 3/6) 103 | expect_equal(calculate_prob_from_vector(v2,2), 0) 104 | expect_equal(calculate_prob_from_vector(v3,1), 1) 105 | expect_equal(calculate_prob_from_vector(v3,0), 0) 106 | expect_equal(calculate_prob_from_vector(v3,2), 0) 107 | expect_equal(calculate_prob_from_vector(v4,1), 0) 108 | expect_equal(calculate_prob_from_vector(v4,0), 1) 109 | expect_equal(calculate_prob_from_vector(v4,2), 0) 110 | }) 111 | 112 | -------------------------------------------------------------------------------- /tests/testthat/test-bpc_helpers_checks.R: -------------------------------------------------------------------------------- 1 | test_that('check_result_column works',{ 2 | v1 <- data.frame(results=c(1,0,2,0,0,1))#true 3 | v2 <- data.frame(results=c(1,1.2,2,0,0,1))#false 4 | v3 <- data.frame(results=c(1,2,-1,0,0,1))#false 5 | v4 <- data.frame(results=c(1,1,2,0,0,1,'a'))#false 6 | expect_true(check_result_column(v1$results)) 7 | expect_false(check_result_column(v2$results)) 8 | expect_false(check_result_column(v3$results)) 9 | expect_false(check_result_column(v4$results)) 10 | }) 11 | 12 | test_that('check_z_column works',{ 13 | v1 <- data.frame(z=c(1,0,1,0,0,1))#true 14 | v2 <- data.frame(z=c(1,1.2,2,0,0,1))#false 15 | v3 <- data.frame(z=c(1,2,-1,0,0,1))#false 16 | v4 <- data.frame(z=c(1,1,2,0,0,1,'a'))#false 17 | expect_true(check_z_column(v1$z)) 18 | expect_false(check_z_column(v2$z)) 19 | expect_false(check_z_column(v3$z)) 20 | expect_false(check_z_column(v4$z)) 21 | }) 22 | 23 | test_that('check_if_there_are_ties works',{ 24 | v1 <- data.frame(results=c(1,0,2,0,0,1))#true 25 | v2 <- data.frame(results=c(1,1,1,0,0,1))#false 26 | v3 <- data.frame(results=c(1,1,0,0,1))#false 27 | expect_true(check_if_there_are_ties(v1$results)) 28 | expect_false(check_if_there_are_ties(v2$results)) 29 | expect_false(check_if_there_are_ties(v3$results)) 30 | }) 31 | 32 | test_that('check_if_there_are_na works',{ 33 | #dataset with Na 34 | test_bt <- load_testdata('test_bt') 35 | test_bt_na1 <- test_bt 36 | test_bt_na1$y[4]<-NA 37 | 38 | test_btscores <- load_testdata('test_btscores') 39 | test_btscores_na1 <- test_btscores 40 | test_btscores_na1$score0[4]<-NA 41 | test_btscores_na1$score1[6]<-NA 42 | 43 | expect_true(check_if_there_are_na(d=test_btscores_na1, 44 | player0='player0', 45 | player1='player1', 46 | player0_score='score0', 47 | player1_score='score1', 48 | result_column=NULL)) 49 | 50 | expect_true(check_if_there_are_na(d=test_bt_na1, 51 | player0='player0', 52 | player1='player1', 53 | result_column='y')) 54 | 55 | expect_false(check_if_there_are_na(d=test_btscores, 56 | player0='player0', 57 | player1='player1', 58 | player0_score='score0', 59 | player1_score='score1', 60 | result_column=NULL)) 61 | 62 | expect_false(check_if_there_are_na(d=test_bt, 63 | player0='player0', 64 | player1='player1', 65 | result_column='y')) 66 | }) 67 | 68 | 69 | test_that('check_numeric_predictor_matrix works',{ 70 | m1<-as.matrix(data.frame(Pred1=c(2.3,1.4,4.2), 71 | Pred2=c(-3.2,0.5,-2.1), 72 | Pred3=c(0.01,0.04,0.02), 73 | Pred4=c(-0.5,-0.2,-0.3))) 74 | 75 | m2<-as.matrix(data.frame(Pred1=c(2.3,1.4,4.2), 76 | Pred2=c(-3.2,0.5,-2.1), 77 | Pred3=c(0.01,'a',0.02), 78 | Pred4=c(-0.5,-0.2,-0.3))) 79 | 80 | m3<-as.matrix(data.frame(Pred1=c(2.3,1.4,4.2), 81 | Pred2=c(-3.2,0.5,-2.1), 82 | Pred3=c(0.01,0,0.02), 83 | Pred4=c(-0.5,-0.2,NA))) 84 | 85 | m4<-as.matrix(data.frame(Pred1=c(2.3,1.4,4.2), 86 | Pred2=c(-3.2,0.5,-2.1), 87 | Pred3=c(0.01,0,0.02), 88 | Pred4=c(TRUE,-0.2,2))) 89 | 90 | m5<-as.matrix(data.frame(Pred1=c(2.3,1.4,4.2), 91 | Pred2=c(-3.2,FALSE,-2.1), 92 | Pred3=c(0.01,0,0.02), 93 | Pred4=c(2,-0.2,2))) 94 | 95 | expect_true(check_numeric_predictor_matrix(m1)) 96 | expect_false(check_numeric_predictor_matrix(m2)) 97 | expect_false(check_numeric_predictor_matrix(m3)) 98 | expect_true(check_numeric_predictor_matrix(m4)) 99 | expect_equal(m4[[1,4]],1) 100 | expect_true(check_numeric_predictor_matrix(m5)) 101 | expect_equal(m5[[2,2]],0) 102 | 103 | }) 104 | 105 | test_that('check_predictors_df_contains_all_players works',{ 106 | test_predictors1<-tibble::tribble(~Player, ~Pred1, ~Pred2, ~Pred3, ~Pred4, 107 | 'A', 2.3, -3.2, 0.01, -1/2, 108 | 'C', 4.2, -2.1, 0.02, -0.3, 109 | 'B', 1.4, 0.5, 0.04, -0.2) 110 | 111 | test_predictors2<-tibble::tribble(~Player, ~Pred1, ~Pred2, ~Pred3, ~Pred4, 112 | 'A', 2.3, -3.2, 0.01, -1/2, 113 | 'C', 4.2, -2.1, 0.02, -0.3, 114 | 'D', 1.4, 0.5, 0.04, -0.2) 115 | 116 | test_predictors3<-tibble::tribble(~Player, ~Pred1, ~Pred2, ~Pred3, ~Pred4, 117 | 'A', 2.3, -3.2, 0.01, -1/2, 118 | 'C', 4.2, -2.1, 0.02, -0.3, 119 | 'D', 1.4, 0.5, 0.04, -0.2, 120 | 'D', 1.4, 0.5, 2, -0.2) 121 | 122 | test_predictors4<-tibble::tribble(~Player, ~Pred1, ~Pred2, ~Pred3, ~Pred4, 123 | 'A', 2.3, -3.2, 0.01, -1/2, 124 | 'C', 4.2, -2.1, 0.02, -0.3, 125 | 'B', 1.4, 0.5, 0.04, -0.2, 126 | 'D', 1.4, 0.5, 2, -0.2) 127 | 128 | 129 | lookup_table<-data.frame(Names=c('A','B','C'), Index=c(1,2,3)) 130 | 131 | expect_true(check_predictors_df_contains_all_players(test_predictors1,lookup_table)) 132 | expect_false(check_predictors_df_contains_all_players(test_predictors2,lookup_table)) 133 | expect_false(check_predictors_df_contains_all_players(test_predictors3,lookup_table)) 134 | expect_false(check_predictors_df_contains_all_players(test_predictors4,lookup_table)) 135 | }) 136 | 137 | -------------------------------------------------------------------------------- /tests/testthat/test-bpc_helpers_hpd.R: -------------------------------------------------------------------------------- 1 | test_that("multiplication works", { 2 | expect_equal(2 * 2, 4) 3 | }) 4 | -------------------------------------------------------------------------------- /tests/testthat/test-bpc_helpers_indexes.R: -------------------------------------------------------------------------------- 1 | test_that("create_index_lookuptable works with datasets", { 2 | test_bt<-load_testdata('test_bt') 3 | 4 | t_test_bt<- as.data.frame(tibble::tribble(~Names, ~Index, 5 | 'A', 1, 6 | 'B', 2, 7 | 'C', 3,)) 8 | 9 | expect_equal(create_index_lookuptable(test_bt, player0='player0', player1='player1'), t_test_bt) 10 | 11 | }) 12 | 13 | test_that("create_index_lookuptable works with numbers", { 14 | number_in_order <- tibble::tribble(~player0, ~player1, 15 | 1, 3, 16 | 2,3, 17 | 3,4) 18 | 19 | number_out_order <- tibble::tribble(~player0, ~player1, 20 | 3, 1, 21 | 4,3, 22 | 1,4, 23 | 5,2) 24 | 25 | t_number_in_order<- as.data.frame(tibble::tribble(~Names, ~Index, 26 | 1, 1, 27 | 2, 2, 28 | 3, 3, 29 | 4, 4)) 30 | t_number_out_order<- as.data.frame(tibble::tribble(~Names, ~Index, 31 | 3, 1, 32 | 4, 2, 33 | 1, 3, 34 | 5, 4, 35 | 2, 5)) 36 | 37 | expect_equal(create_index_lookuptable(number_in_order, player0='player0', player1='player1'), t_number_in_order) 38 | expect_equal(create_index_lookuptable(number_out_order, player0='player0', player1='player1'), t_number_out_order) 39 | }) 40 | 41 | test_that("create_index works", { 42 | test_bt<-load_testdata('test_bt') 43 | t_test_bt<- as.data.frame(tibble::tribble(~player0, ~player1, ~y, ~player0_index, ~player1_index, 44 | 'A', 'B', 0, 1, 2, 45 | 'A', 'B', 0, 1, 2, 46 | 'A', 'B', 1, 1, 2, 47 | 'A', 'C', 0, 1, 3, 48 | 'A', 'C', 0, 1, 3, 49 | 'A', 'C', 0, 1, 3, 50 | 'B', 'C', 0, 2, 3, 51 | 'B', 'C', 0, 2, 3, 52 | 'B', 'C', 1, 2, 3)) 53 | expect_equal(create_index(test_bt, player0='player0', player1='player1'), t_test_bt) 54 | }) 55 | 56 | 57 | 58 | test_that('replace_parameter_index_with_names works',{ 59 | test_bt<-load_testdata('test_bt') 60 | data<-data.frame(v= c('lambda[1]','lambda[2]','lambda[3]')) 61 | lookup_table<-create_index_lookuptable(test_bt,player0 = 'player0',player1 = 'player1') 62 | new_data <- data.frame(v=c('lambda[A]', 'lambda[B]', 'lambda[C]')) 63 | 64 | expect_equal(replace_parameter_index_with_names(data,'v','lambda',lookup_table),new_data) 65 | }) 66 | 67 | test_that('create_array_of_par_names works',{ 68 | test_bt<-load_testdata('test_bt') 69 | test_btU<-load_testdata('test_btU') 70 | test_bt_subject<-load_testdata('test_bt_subject') 71 | lookup_table<-create_index_lookuptable(test_bt, player0 = 'player0',player1 = 'player1') 72 | result <- c('lambda[A]', 'lambda[B]', 'lambda[C]') 73 | expect_equal(create_array_of_par_names(par='lambda',lookup_table),result) 74 | 75 | lookup_table2<-create_index_lookuptable(test_btU, player0 = 'player0',player1 = 'player1') 76 | cluster_lookup_table2<-create_index_cluster_lookuptable(test_btU, cluster='cluster') 77 | result2 <- c('U1[A,c1]', 'U1[B,c1]', 'U1[C,c1]','U1[A,c2]', 'U1[B,c2]', 'U1[C,c2]','U1[A,c3]', 'U1[B,c3]', 'U1[C,c3]','U1[A,c4]', 'U1[B,c4]', 'U1[C,c4]') 78 | expect_equal(create_array_of_par_names(par='U1',lookup_table=lookup_table2, cluster_lookup_table = list(cluster_lookup_table2)) ,result2) 79 | 80 | lookup_table3<-create_index_lookuptable(test_bt_subject, player0 = 'player0', player1 = 'player1') 81 | subject_predictor_lookuptable<-create_subject_predictor_lookuptable(subject_predictors = c('SPred1', 'SPred2', 'SPred3')) 82 | result3 <- c('S[A,SPred1]', 'S[B,SPred1]', 'S[C,SPred1]','S[A,SPred2]', 'S[B,SPred2]', 'S[C,SPred2]','S[A,SPred3]', 'S[B,SPred3]', 'S[C,SPred3]') 83 | expect_equal(create_array_of_par_names(par='S',lookup_table=lookup_table3, subject_predictors_lookup_table =subject_predictor_lookuptable) ,result3) 84 | }) 85 | 86 | 87 | test_that('create_predictor_matrix_with_player_lookup_table works',{ 88 | test_predictors<-load_testdata('test_predictors') 89 | result<-as.matrix(data.frame(Pred1=c(2.3,1.4,4.2), 90 | Pred2=c(-3.2,0.5,-2.1), 91 | Pred3=c(0.01,0.04,0.02), 92 | Pred4=c(-0.5,-0.2,-0.3))) 93 | predictor_all_columns<-colnames(test_predictors) 94 | predictors_columns <- predictor_all_columns[2:length(predictor_all_columns)] 95 | player_column <- predictor_all_columns[1] 96 | lookup_table<-data.frame(Names=c('A','B','C'), Index=c(1,2,3)) 97 | pred_matrix<-create_predictor_matrix_with_player_lookup_table(d=test_predictors, 98 | player=player_column, 99 | predictors_columns=predictors_columns, 100 | lookup_table=lookup_table) 101 | expect_equal(pred_matrix,result) 102 | 103 | }) 104 | 105 | test_that('create_predictors_lookup_table works',{ 106 | test_predictors<-load_testdata('test_predictors') 107 | result<-data.frame(Names=c('Pred1','Pred2','Pred3','Pred4'), 108 | Index=c(1,2,3,4)) 109 | predictor_all_columns<-colnames(test_predictors) 110 | predictors_columns <- predictor_all_columns[2:length(predictor_all_columns)] 111 | predictors_lookup_table<-create_predictors_lookup_table(predictors_columns) 112 | expect_equal(predictors_lookup_table,result) 113 | 114 | }) 115 | 116 | test_that('create_index_predictors_with_lookup_table works',{ 117 | test_predictors<-load_testdata('test_predictors') 118 | result<-cbind(test_predictors,player_index=c(1,3,2)) 119 | 120 | predictor_all_columns<-colnames(test_predictors) 121 | predictors_columns <- predictor_all_columns[2:length(predictor_all_columns)] 122 | player_column <- predictor_all_columns[1] 123 | lookup_table<-data.frame(Names=c('A','B','C'), Index=c(1,2,3)) 124 | predictors_with_indexes<-create_index_predictors_with_lookup_table(test_predictors, player=player_column, lookup_table=lookup_table) 125 | expect_equal(predictors_with_indexes,result) 126 | 127 | }) 128 | 129 | 130 | 131 | -------------------------------------------------------------------------------- /tests/testthat/test-bpc_object.R: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidissamattos/bpcs/4c5b1015669717723ad4d652c8743eed3ef3b73b/tests/testthat/test-bpc_object.R -------------------------------------------------------------------------------- /tests/testthat/test-stan_bt-subjects.R: -------------------------------------------------------------------------------- 1 | test_that("bpc returns a bpc object the bt model", { 2 | skip_on_cran() 3 | test_bt_subject <- load_testdata('test_bt_subject') 4 | m1 <- bpc( 5 | data = test_bt_subject, 6 | player0 = 'player0', 7 | player1 = 'player1', 8 | result_column = 'y', 9 | model_type = 'bt-subjectpredictors', 10 | subject_predictors = c('SPred1', 'SPred2', 'SPred3'), 11 | solve_ties = 'random', 12 | win_score = 'higher', 13 | iter = 1000, 14 | warmup = 300, 15 | show_chain_messages = F, 16 | seed = 8484 17 | ) 18 | # Sys.sleep(5) 19 | expect_s3_class(m1, 'bpc') 20 | expect_no_error(posterior_predictive(m1)) 21 | expect_no_error(summary(m1)) 22 | 23 | }) 24 | -------------------------------------------------------------------------------- /tests/testthat/test-stan_bt.R: -------------------------------------------------------------------------------- 1 | test_that("bpc returns a bpc object the bt model", { 2 | skip_on_cran() 3 | test_bt <- load_testdata('test_bt') 4 | 5 | m1 <- bpc( 6 | data = test_bt, 7 | player0 = 'player0', 8 | player1 = 'player1', 9 | result_column = 'y', 10 | model_type = 'bt', 11 | solve_ties = 'random', 12 | win_score = 'higher', 13 | iter = 1000, 14 | warmup = 300, 15 | show_chain_messages = F, 16 | seed = 8484 17 | ) 18 | # Sys.sleep(5) 19 | test_btscores <- load_testdata('test_btscores') 20 | m2 <- bpc( 21 | data = test_btscores, 22 | player0 = 'player0', 23 | player1 = 'player1', 24 | player0_score = 'score0', 25 | player1_score = 'score1', 26 | model_type = 'bt', 27 | solve_ties = 'random', 28 | win_score = 'higher', 29 | iter = 1000, 30 | warmup = 300, 31 | show_chain_messages = F, 32 | seed = 8484 33 | ) 34 | 35 | expect_s3_class(m1, 'bpc') 36 | expect_s3_class(m2, 'bpc') 37 | expect_no_error(summary(m1)) 38 | expect_no_error(summary(m2)) 39 | expect_no_error(check_convergence_diagnostics(m1)) 40 | expect_no_error(check_convergence_diagnostics(m2)) 41 | 42 | 43 | # testing with solve ties 44 | 45 | dt <- dplyr::bind_rows( 46 | tennis_agresti, 47 | tibble::tibble(player0 = "Seles", player1 = "Graf", y = 2, id = 47) 48 | ) 49 | 50 | m3 <- bpc(data = dt, 51 | player0 = 'player0', 52 | player1 = 'player1', 53 | result_column = 'y', 54 | model_type = 'bt', 55 | solve_ties = 'random', 56 | show_chain_messages = T) 57 | m4 <- bpc(data = dt, 58 | player0 = 'player0', 59 | player1 = 'player1', 60 | result_column = 'y', 61 | model_type = 'bt', 62 | solve_ties = 'remove', 63 | show_chain_messages = T) 64 | expect_s3_class(m3, 'bpc') 65 | expect_s3_class(m4, 'bpc') 66 | expect_no_error(summary(m3)) 67 | expect_no_error(summary(m4)) 68 | expect_no_error(check_convergence_diagnostics(m3)) 69 | expect_no_error(check_convergence_diagnostics(m4)) 70 | 71 | 72 | 73 | }) 74 | -------------------------------------------------------------------------------- /tests/testthat/test-stan_btU.R: -------------------------------------------------------------------------------- 1 | test_that("bpc returns a bpc object the btU model", { 2 | skip_on_cran() 3 | test_btU <- load_testdata('test_btU') 4 | m1 <- bpc( 5 | data = test_btU, 6 | player0 = 'player0', 7 | player1 = 'player1', 8 | result_column = 'y', 9 | cluster = c('cluster'), 10 | model_type = 'bt-U', 11 | solve_ties = 'random', 12 | win_score = 'higher', 13 | iter = 2000, 14 | warmup = 500, 15 | show_chain_messages = F, 16 | seed=8484 17 | ) 18 | expect_s3_class(m1, 'bpc') 19 | expect_no_error(summary(m1)) 20 | expect_no_error(posterior_predictive(m1)) 21 | expect_no_error(get_probabilities_df(m1, model_type = 'bt')) 22 | 23 | # Sys.sleep(5) 24 | m2 <- bpc( 25 | data = test_btU, 26 | player0 = 'player0', 27 | player1 = 'player1', 28 | result_column = 'y', 29 | cluster = c('cluster', 'cluster2'), 30 | model_type = 'bt-U', 31 | solve_ties = 'random', 32 | win_score = 'higher', 33 | iter = 2000, 34 | warmup = 500, 35 | show_chain_messages = F, 36 | seed=8484 37 | ) 38 | expect_s3_class(m2, 'bpc') 39 | expect_no_error(summary(m2)) 40 | 41 | expect_no_error(posterior_predictive(m2)) 42 | expect_no_error(get_probabilities_df(m2, model_type = 'bt')) 43 | 44 | # Sys.sleep(5) 45 | m3 <- bpc( 46 | data = test_btU, 47 | player0 = 'player0', 48 | player1 = 'player1', 49 | result_column = 'y', 50 | cluster = c('cluster', 'cluster2','cluster3'), 51 | model_type = 'bt-U', 52 | solve_ties = 'random', 53 | win_score = 'higher', 54 | iter = 2000, 55 | warmup = 500, 56 | show_chain_messages = F, 57 | seed=8484 58 | ) 59 | # Sys.sleep(5) 60 | expect_s3_class(m3, 'bpc') 61 | expect_no_error(summary(m3)) 62 | expect_no_error(posterior_predictive(m3)) 63 | expect_no_error(get_probabilities_df(m3, model_type = 'bt')) 64 | }) 65 | -------------------------------------------------------------------------------- /tests/testthat/test-stan_btUordereffect.R: -------------------------------------------------------------------------------- 1 | test_that("bpc returns a bpc object the bt-U-ordereffect model", { 2 | skip_on_cran() 3 | test_btU <- load_testdata('test_btUordereffect') 4 | m1 <- bpc( 5 | data = test_btU, 6 | player0 = 'player0', 7 | player1 = 'player1', 8 | result_column = 'y', 9 | cluster = 'cluster', 10 | z_player1 = 'z1', 11 | model_type = 'bt-U-ordereffect', 12 | solve_ties = 'random', 13 | win_score = 'higher', 14 | iter = 2000, 15 | warmup = 600, 16 | show_chain_messages = F, 17 | seed = 8484 18 | ) 19 | # Sys.sleep(5) 20 | expect_s3_class(m1, 'bpc') 21 | expect_no_error(summary(m1)) 22 | }) 23 | -------------------------------------------------------------------------------- /tests/testthat/test-stan_btgeneralized.R: -------------------------------------------------------------------------------- 1 | test_that("bpc returns a bpc object the btgeneralized model", { 2 | skip_on_cran() 3 | test_bt <- load_testdata('test_bt') 4 | test_predictors <- load_testdata('test_predictors') 5 | m1 <- bpc( 6 | data = test_bt, 7 | player0 = 'player0', 8 | player1 = 'player1', 9 | result_column = 'y', 10 | model_type = 'bt-generalized', 11 | predictors = test_predictors, 12 | solve_ties = 'none', 13 | iter = 1000, 14 | warmup = 300, 15 | show_chain_messages = F, 16 | seed = 8484 17 | ) 18 | 19 | # Sys.sleep(5) 20 | expect_s3_class(m1, 'bpc') 21 | expect_no_error(summary(m1)) 22 | expect_no_error(posterior_predictive(m1)) 23 | 24 | }) 25 | -------------------------------------------------------------------------------- /tests/testthat/test-stan_btordereffect.R: -------------------------------------------------------------------------------- 1 | test_that("bpc returns a bpc object the btordereffect model", { 2 | skip_on_cran() 3 | test_btorder <- load_testdata('test_btorder') 4 | m1 <- bpc( 5 | data = test_btorder, 6 | player0 = 'player0', 7 | player1 = 'player1', 8 | result_column = 'y', 9 | z_player1 = 'z1', 10 | model_type = 'bt-ordereffect', 11 | solve_ties = 'random', 12 | win_score = 'higher', 13 | iter = 1000, 14 | warmup = 300, 15 | show_chain_messages = F, 16 | seed = 8484 17 | ) 18 | # Sys.sleep(5) 19 | expect_s3_class(m1, 'bpc') 20 | expect_no_error(summary(m1)) 21 | 22 | }) 23 | -------------------------------------------------------------------------------- /tests/testthat/test-stan_davidson.R: -------------------------------------------------------------------------------- 1 | test_that("bpc returns a bpc object with datasets using the davidson model", { 2 | skip_on_cran() 3 | test_davidson <- load_testdata('test_davidson') 4 | m1 <- bpc( 5 | data = test_davidson, 6 | player0 = 'player0', 7 | player1 = 'player1', 8 | result_column = 'y', 9 | solve_ties = 'none', 10 | model_type = 'davidson', 11 | iter = 1000, 12 | warmup = 300, 13 | show_chain_messages = F, 14 | seed = 8484 15 | ) 16 | # Sys.sleep(5) 17 | expect_s3_class(m1, 'bpc') 18 | expect_no_error(summary(m1)) 19 | 20 | expect_no_error(posterior_predictive(m1)) 21 | 22 | expect_no_error(get_probabilities_df(m1, model_type = 'davidson')) 23 | }) 24 | -------------------------------------------------------------------------------- /tests/testthat/test-stan_davidsonU.R: -------------------------------------------------------------------------------- 1 | test_that("bpc returns a bpc object the davidsonU model", { 2 | skip_on_cran() 3 | test_davidsonU <- load_testdata('test_davidsonU') 4 | m1 <- bpc( 5 | data = test_davidsonU, 6 | player0 = 'player0', 7 | player1 = 'player1', 8 | result_column = 'y', 9 | cluster = c('cluster'), 10 | model_type = 'davidson-U', 11 | solve_ties = 'none', 12 | win_score = 'higher', 13 | iter = 1000, 14 | warmup = 300, 15 | show_chain_messages = F, 16 | seed=8484 17 | ) 18 | # Sys.sleep(5) 19 | expect_s3_class(m1, 'bpc') 20 | expect_no_error(summary(m1)) 21 | expect_no_error(posterior_predictive(m1)) 22 | expect_no_error(get_probabilities_df(m1, model_type = 'bt')) 23 | 24 | m2 <- bpc( 25 | data = test_davidsonU, 26 | player0 = 'player0', 27 | player1 = 'player1', 28 | result_column = 'y', 29 | cluster = c('cluster', 'cluster2'), 30 | model_type = 'davidson-U', 31 | solve_ties = 'none', 32 | win_score = 'higher', 33 | iter = 1200, 34 | warmup = 500, 35 | show_chain_messages = F, 36 | seed=8484 37 | ) 38 | 39 | expect_s3_class(m2, 'bpc') 40 | expect_no_error(summary(m2)) 41 | expect_no_error(posterior_predictive(m2)) 42 | expect_no_error(get_probabilities_df(m2, model_type = 'bt')) 43 | # Sys.sleep(5) 44 | m3 <- bpc( 45 | data = test_davidsonU, 46 | player0 = 'player0', 47 | player1 = 'player1', 48 | result_column = 'y', 49 | cluster = c('cluster', 'cluster2', 'cluster3'), 50 | model_type = 'davidson-U', 51 | solve_ties = 'none', 52 | win_score = 'higher', 53 | iter = 1000, 54 | warmup = 300, 55 | show_chain_messages = F, 56 | seed=8484 57 | ) 58 | # Sys.sleep(5) 59 | expect_s3_class(m3, 'bpc') 60 | expect_no_error(summary(m3)) 61 | expect_no_error(posterior_predictive(m3)) 62 | expect_no_error(get_probabilities_df(m3, model_type = 'bt')) 63 | }) 64 | -------------------------------------------------------------------------------- /tests/testthat/test-stan_davidsongeneralized.R: -------------------------------------------------------------------------------- 1 | test_that("bpc returns a bpc object the davidsongeneralized model", { 2 | skip_on_cran() 3 | test_davidson <- load_testdata('test_davidson') 4 | test_predictors <- load_testdata('test_predictors') 5 | m1 <- bpc( 6 | data = test_davidson, 7 | player0 = 'player0', 8 | player1 = 'player1', 9 | result_column = 'y', 10 | model_type = 'davidson-generalized', 11 | predictors = test_predictors, 12 | solve_ties = 'none', 13 | iter = 1000, 14 | warmup = 300, 15 | show_chain_messages = F, 16 | seed=8484 17 | ) 18 | 19 | # Sys.sleep(5) 20 | expect_s3_class(m1, 'bpc') 21 | expect_no_error(summary(m1)) 22 | 23 | }) 24 | -------------------------------------------------------------------------------- /tests/testthat/test-stan_davidsonordereffect.R: -------------------------------------------------------------------------------- 1 | test_that("bpc returns a bpc object the davidsonordereffect model", { 2 | skip_on_cran() 3 | test_davidsonorder <- load_testdata('test_davidsonorder') 4 | m1 <- bpc( 5 | data = test_davidsonorder, 6 | player0 = 'player0', 7 | player1 = 'player1', 8 | result_column = 'y', 9 | z_player1 = 'z1', 10 | model_type = 'davidson-ordereffect', 11 | solve_ties = 'none', 12 | win_score = 'higher', 13 | iter = 1000, 14 | warmup = 300, 15 | show_chain_messages = F, 16 | seed = 8484 17 | ) 18 | # Sys.sleep(5) 19 | expect_s3_class(m1, 'bpc') 20 | expect_no_error(summary(m1)) 21 | }) 22 | -------------------------------------------------------------------------------- /vignettes/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | *.R 3 | -------------------------------------------------------------------------------- /vignettes/Instructions.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Instructions" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{Instructions} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | 11 | Instructions for installation, use, and a paper with descriptions of the statistical models are available at the documentation website: https://davidissamattos.github.io/bpcs/index.html 12 | -------------------------------------------------------------------------------- /vignettes/a_get_started.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Getting Started with the bpcs package" 3 | bibliography: bibliography.bib 4 | --- 5 | 6 | ```{r, include = FALSE} 7 | knitr::opts_chunk$set( 8 | collapse = TRUE, 9 | comment = "#>" 10 | ) 11 | ``` 12 | 13 | ```{r setup, echo=T, results='hide', warning=F, message=F} 14 | library(bpcs) 15 | library(ggplot2) 16 | library(dplyr) 17 | library(tibble) 18 | library(kableExtra) 19 | library(bayesplot) 20 | library(knitr) 21 | ``` 22 | 23 | # Getting started 24 | In this vignette, we provide an example of the usage of the `bpcs` package along with the core concepts to use the package. 25 | 26 | ## Installation 27 | The package requires installation of the `rstan` package [@rstan]. For more details see the `REAMDE.md` document. 28 | 29 | To install the latest version from Github: 30 | ```{r eval=FALSE, echo=T} 31 | remotes::install_github('davidissamattos/bpcs') 32 | ``` 33 | 34 | After install we load the package with: 35 | ```{r} 36 | library(bpcs) 37 | ``` 38 | 39 | ## Introduction 40 | 41 | The `bpcs` package performs Bayesian estimation of Paired Comparison models utilizing Stan. 42 | We provide a series of models and auxiliary functions to help in the analysis and evaluation of the models. However, this package have the philosophy of 'batteries not included' for plots, tables and data transformation. There are already many great packages capable of performing create high quality plots, tables and that provides tools for data transformation. Since each user can have their own preferences, customization needs and data cleaning and transformation workflows, we designed not to enforce any particular framework or package. Our functions were designed to return cleaned data frames that can be used almost directly, or with few transformations in those packages. 43 | 44 | At the moment, the only exception to this is the `expand_aggregated_data` function that receives a data frame with the number of wins for player 1 and the numbers of wins for player 2 and expand this aggregated data into a single match per row (that is required for our models). We include this function because this type of transformation is common since packages such as `BradleyTerry2` [@turner2012bradley] receives this type of aggregated data and many available datasets are presented like that. 45 | 46 | With that said, we provide in the vignettes the code we use to transform the data and generate the tables and plots. The user is free to use/copy/modify these codes for their own use. For those we rely on the collection of packages `tidyverse` [@tidyverse2019], and the packages `knitr` [@knitr2014] and `kableExtra` [@kableExtra2020]. 47 | 48 | 49 | # The Bradley Terry model 50 | 51 | # Tennis example 52 | 53 | In this example, we will use the example from tennis players from Agresti [@agresti2003categorical]. The data `tennis_agresti` contains the information regarding tennis matches between 5 players, and who won the match, 0 for player0 or 1 for player1. 54 | 55 | ```{r} 56 | knitr::kable(tennis_agresti) %>% 57 | kableExtra::kable_styling() 58 | ``` 59 | 60 | We can fit a Bayesian Bradley-Terry model using the `bpc` function 61 | 62 | ```{r} 63 | m1 <- bpc(data = tennis_agresti, 64 | player0 = 'player0', 65 | player1 = 'player1', 66 | result_column = 'y', 67 | model_type = 'bt', 68 | solve_ties = 'none', #there are no ties 69 | show_chain_messages = T) 70 | ``` 71 | 72 | ## Diagnostics 73 | 74 | After the chain converges to find the result we can investigate if everything went right. 75 | For that we can use the excellent tool provided in the `shinystan` [@shinystan2018] package that helps to assess the convergence of the chains. 76 | 77 | The `bpcs` package provides a tiny wrapper to launch it automatically with some default parameters. 78 | 79 | ```{r eval=F} 80 | launch_shinystan(m1) 81 | ``` 82 | 83 | If you prefer to investigate without `shinystan` we can retrieve the cmdstanr fit object and investigate ourselves or with the help of the `bayesplot` package [@bayesplot2019]. The indexes in Stan refer to the names and indexes available at the lookup table. 84 | 85 | ```{r} 86 | knitr::kable(m1$lookup_table) 87 | ``` 88 | 89 | 90 | ```{r} 91 | fit <- get_fit(m1) 92 | posterior_draws <- posterior::as_draws_matrix(fit$draws()) 93 | ``` 94 | 95 | Getting the traceplots: 96 | 97 | ```{r eval=T} 98 | bayesplot::mcmc_trace(posterior_draws,pars = c("lambda[1]","lambda[2]","lambda[3]","lambda[4]"), n_warmup=1000) 99 | ``` 100 | 101 | Verifying the Rhat and neff 102 | ```{r} 103 | get_parameters_df(m1, n_eff = T, Rhat = T) 104 | ``` 105 | 106 | 107 | ## Predictive posterior 108 | 109 | We first get the observed values and then the predictive values of the original dataframe. We can get predictive values with the predictive function and passing a data frame with the values we want to predict (in this case the original one). Note that we need to have the same column names in this new data frame 110 | 111 | ```{r} 112 | y_pp <- posterior_predictive(m1) 113 | y<-as.vector(y_pp$y) 114 | y_pred <- y_pp$y_pred 115 | ``` 116 | 117 | 118 | ```{r} 119 | bayesplot::ppc_bars(y=y, yrep=y_pred) + 120 | labs(title = 'Bar plot with medians and uncertainty\n intervals superimposed') 121 | ``` 122 | 123 | The plots indicate a good model as the predictive posterior and the observed values agree largely. 124 | 125 | ## Parameter tables and plots 126 | 127 | Now that we are confident that our model is correct, we can create some tables to report our results. 128 | 129 | To see the results in the console the `summary` function provides a good overview of the model. With parameters, probability of winning and a ranking. 130 | ```{r} 131 | summary(m1) 132 | ``` 133 | 134 | If we want to create nicer tables and export them to latex/html we can leverage this with the `kable` function and the `kableExtra` package. Note that for extensive customization (and examples) we refer to the packages documentation. 135 | 136 | Parameter table with HPD intervals 137 | ```{r} 138 | get_parameters_table(m1, format='html') %>% 139 | kable_styling() 140 | ``` 141 | 142 | Plot the HPD intervals of the strength 143 | ```{r} 144 | plot(m1, rotate_x_labels = T) 145 | ``` 146 | 147 | 148 | ```{r} 149 | get_probabilities_table(m1, format='html') %>% 150 | kableExtra::kable_styling() 151 | ``` 152 | We might also be interested in ranking the players based on their ability $lambda$. In the Bayesian case, we sample the posterior distribution of $lambda$ and rank them so we have posterior distribution of the ranks. 153 | 154 | We can produce a table with the values of this dataframe. 155 | 156 | ```{r} 157 | get_rank_of_players_table(m1, format='html') %>% 158 | kable_styling() 159 | ``` 160 | 161 | ## Predicting results 162 | 163 | To predict new results we need a data frame similar to the one used to fit the data. We use the same function as in the predicted posterior but now we provide the data we want to predict instead of the original data. Lets predict who is the winner for all games from Seles. Now we don't want to return the matrix but a data frame 164 | 165 | ```{r} 166 | tennis_new_games<- tibble::tribble(~player0, ~player1, 167 | 'Seles', 'Graf', 168 | 'Seles', 'Sabatini', 169 | 'Seles', 'Navratilova', 170 | 'Seles', 'Sanchez') 171 | y_seles<-predict(m1,tennis_new_games,n=100,return_matrix = T) 172 | print(head(y_seles)) 173 | ``` 174 | 175 | We can process this predictive posterior as desired. The summary function already gives the predictive probabilities to facilitate. 176 | 177 | # References 178 | -------------------------------------------------------------------------------- /vignettes/b_ties_and_home_advantage.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Home advantage in the Bradley-Terry and in the Davidson model" 3 | bibliography: bibliography.bib 4 | --- 5 | 6 | ```{r, include = FALSE} 7 | knitr::opts_chunk$set( 8 | collapse = TRUE, 9 | comment = "#>" 10 | ) 11 | ``` 12 | 13 | ```{r setup, warning=F, message=F} 14 | library(bpcs) 15 | library(knitr) 16 | library(dplyr) 17 | library(ggplot2) 18 | library(kableExtra) 19 | ``` 20 | 21 | # Introduction 22 | 23 | In this vignette, we will go over a sport dataset that consists of the games from the main Brazilian football league from 2017-2019. In this example, we will create a ranking system for the teams based on the Bradley-Terry model. Then we will expand this to include ties, home-advantage effects and ties with home-advantage. Note that in this example we give equal weights to a game regardless of the date, i.e. more recent games have the same impact as older games. 24 | 25 | The data can be accessed by: 26 | 27 | ```{r} 28 | data("brasil_soccer_league") 29 | knitr::kable(head(brasil_soccer_league)) 30 | ``` 31 | 32 | Let's analyze only the data from 2019 and remove a few columns that are not relevant for this example: 33 | ```{r} 34 | d <- brasil_soccer_league %>% 35 | dplyr::filter(Date >= as.Date("2019-01-01") & 36 | Date <= as.Date("2019-12-31")) %>% 37 | dplyr::select(HomeTeam, VisitorTeam, ScoreHomeTeam, ScoreVisitorTeam, Round) 38 | ``` 39 | 40 | Now we have a smaller dataset (380 rows with 5 variables) 41 | 42 | ```{r} 43 | knitr::kable(head(d)) 44 | ``` 45 | 46 | # Fitting a Bradley-Terry model 47 | 48 | Let's start fitting a simple Bradley-Terry model and handle ties randomly 49 | 50 | ```{r} 51 | m1 <- bpc( 52 | d, 53 | player0 = 'VisitorTeam', 54 | player1 = 'HomeTeam', 55 | player0_score = 'ScoreVisitorTeam', 56 | player1_score = 'ScoreHomeTeam', 57 | model_type = 'bt', 58 | solve_ties = 'random', 59 | priors = list(prior_lambda_std = 2.0), 60 | # making a more informative prior to improve convergence 61 | iter = 3000 62 | ) #stan indicates a low bulk ESS so we are increasing the number of iterations 63 | ``` 64 | 65 | ## Simple diagnostics 66 | 67 | Looking at the Rhat and the n_eff: 68 | ```{r} 69 | get_parameters_df(m1, n_eff = T, Rhat = T) 70 | ``` 71 | 72 | Both look fine for all teams. 73 | 74 | Looking at the traceplots for the first 4 teams only (we can look at the others or launch the shinystan app) 75 | 76 | ```{r} 77 | fit <- get_fit(m1) 78 | posterior_draws <- posterior::as_draws_matrix(fit$draws()) 79 | bayesplot::mcmc_trace(posterior_draws,pars = c("lambda[1]","lambda[2]","lambda[3]","lambda[4]"), n_warmup=1000) 80 | ``` 81 | 82 | They sound ok so there is no reason why we should not trust our data 83 | 84 | ## Ranking with the bt model 85 | 86 | Let's get the rank with the simple `bt` model 87 | 88 | ```{r} 89 | get_rank_of_players_table(m1, format = 'html') 90 | ``` 91 | 92 | # Fitting the Davidson model 93 | 94 | Now lets investigate how ties impact our model 95 | 96 | ```{r} 97 | m2 <- bpc(d, 98 | player0 = 'VisitorTeam', 99 | player1 = 'HomeTeam', 100 | player0_score = 'ScoreVisitorTeam', 101 | player1_score = 'ScoreHomeTeam', 102 | model_type = 'davidson', 103 | solve_ties = 'none', 104 | priors = list(prior_lambda_std=2.0), # making a more informative prior to improve convergence 105 | iter = 3000) #stan indicates a low bulk ESS so we are increasing the number of iterations 106 | ``` 107 | 108 | For sake of space and repetition we will not present the diagnostics which can be observed at: 109 | ```{r eval=F} 110 | launch_shinystan(m2) 111 | ``` 112 | 113 | 114 | Let's look at the parameters 115 | ```{r} 116 | print(m2) 117 | ``` 118 | 119 | ## Ranking 120 | 121 | Let's look at the ranking with ties: 122 | 123 | ```{r} 124 | get_rank_of_players_table(m2, format = 'html') 125 | ``` 126 | 127 | We can see that when we consider ties the rank has changed a bit and the difference between the teams reduce (we can see from both the parameter table as well as many equal median ranks between the teams). 128 | 129 | # Bradley-Terry with order effect (home advantage) 130 | 131 | 132 | ```{r} 133 | d_home <- d %>% 134 | dplyr::mutate(home_player1 = 1) 135 | 136 | m3 <- bpc(d_home, 137 | player0 = 'VisitorTeam', 138 | player1 = 'HomeTeam', 139 | player0_score = 'ScoreVisitorTeam', 140 | player1_score = 'ScoreHomeTeam', 141 | z_player1 = 'home_player1', 142 | model_type = 'bt-ordereffect', 143 | solve_ties = 'random', 144 | priors = list(prior_lambda_std=2.0), # making a more informative prior to improve convergence 145 | iter = 3000) #stan indicates a low bulk ESS so we are increasing the number of iterations 146 | ``` 147 | 148 | For sake of space and repetition we will not present the diagnostics which can be observed at: 149 | ```{r eval=F} 150 | launch_shinystan(m3) 151 | ``` 152 | 153 | Let's look at the parameters 154 | ```{r} 155 | print(m3) 156 | ``` 157 | 158 | We can see that the gm parameter is negative indicating that playing home indeed provide an advantage to the matches. 159 | 160 | ## Ranking 161 | 162 | Let's look at the ranking with home advantage: 163 | 164 | ```{r} 165 | get_rank_of_players_table(m3, format = 'html') 166 | ``` 167 | 168 | We can see that the players ranking has changed a bit from the BT and the Davidson model when we compensate for the home advantage 169 | 170 | # Davidson with order effect (home advantage) 171 | 172 | Now let's fit our last model. The Davidson model with order effect. Here we take into account the ties and the home advantage effect 173 | 174 | ```{r} 175 | m4 <- bpc(d_home, 176 | player0 = 'VisitorTeam', 177 | player1 = 'HomeTeam', 178 | player0_score = 'ScoreVisitorTeam', 179 | player1_score = 'ScoreHomeTeam', 180 | z_player1 = 'home_player1', 181 | model_type = 'davidson-ordereffect', 182 | solve_ties = 'none', 183 | priors = list(prior_lambda_std=2.0), # making a more informative prior to improve convergence 184 | iter = 3000) #stan indicates a low bulk ESS so we are increasing the number of iterations 185 | ``` 186 | 187 | For sake of space and repetition we will not present the diagnostics which can be observed at: 188 | ```{r eval=F} 189 | launch_shinystan(m4) 190 | ``` 191 | 192 | Let's look at the parameters 193 | ```{r} 194 | print(m4) 195 | ``` 196 | 197 | We can see again that the home advantage gm parameter was negative, indicating that there is a home advantage effect. 198 | 199 | ## Ranking 200 | 201 | Let's look at the ranking with home advantage and ties: 202 | 203 | ```{r} 204 | get_rank_of_players_table(m4, format = 'html') 205 | ``` 206 | 207 | # Comparing the models with WAIC 208 | 209 | Let's see now using an information criteria (the WAIC) which model fits the data better. 210 | 211 | ```{r} 212 | m1_waic <-get_waic(m1) 213 | m2_waic <-get_waic(m2) 214 | m3_waic <-get_waic(m3) 215 | m4_waic <-get_waic(m4) 216 | ``` 217 | 218 | 219 | We can look at each waic: 220 | ```{r} 221 | m1_waic 222 | m2_waic 223 | m3_waic 224 | m4_waic 225 | ``` 226 | 227 | 228 | Or can also use the loo::compare function to see which performs better. 229 | ```{r} 230 | loo::loo_compare(m1_waic,m2_waic, m3_waic, m4_waic) 231 | ``` 232 | 233 | 234 | -------------------------------------------------------------------------------- /vignettes/bibliography.bib: -------------------------------------------------------------------------------- 1 | @article{turner2012bradley, 2 | title={Bradley-Terry models in R: the BradleyTerry2 package}, 3 | author={Turner, Heather and Firth, David and others}, 4 | journal={Journal of Statistical Software}, 5 | volume={48}, 6 | number={9}, 7 | year={2012}, 8 | publisher={Citeseer} 9 | } 10 | 11 | @article{davidson1970extending, 12 | title={On extending the Bradley-Terry model to accommodate ties in paired comparison experiments}, 13 | author={Davidson, Roger R}, 14 | journal={Journal of the American Statistical Association}, 15 | volume={65}, 16 | number={329}, 17 | pages={317--328}, 18 | year={1970}, 19 | publisher={Taylor \& Francis Group} 20 | } 21 | 22 | @article{bradley1952rank, 23 | title={Rank analysis of incomplete block designs: I. The method of paired comparisons}, 24 | author={Bradley, Ralph Allan and Terry, Milton E}, 25 | journal={Biometrika}, 26 | volume={39}, 27 | number={3/4}, 28 | pages={324--345}, 29 | year={1952}, 30 | publisher={JSTOR} 31 | } 32 | 33 | 34 | @book{agresti2003categorical, 35 | title={Categorical data analysis}, 36 | author={Agresti, Alan}, 37 | volume={482}, 38 | year={2003}, 39 | publisher={John Wiley \& Sons} 40 | } 41 | 42 | @Misc{rstan, 43 | title = {{RStan}: the {R} interface to {Stan}}, 44 | author = {{Stan Development Team}}, 45 | note = {R package version 2.19.3}, 46 | year = {2020}, 47 | url = {https://mc-stan.org/}, 48 | } 49 | 50 | @article{cattelan2012models, 51 | title={Models for paired comparison data: A review with emphasis on dependent data}, 52 | author={Cattelan, Manuela}, 53 | journal={Statistical Science}, 54 | pages={412--433}, 55 | year={2012}, 56 | publisher={JSTOR} 57 | } 58 | 59 | @Article{tidyverse2019, 60 | title = {Welcome to the {tidyverse}}, 61 | author = {Hadley Wickham and Mara Averick and Jennifer Bryan and Winston Chang and Lucy D'Agostino McGowan and Romain François and Garrett Grolemund and Alex Hayes and Lionel Henry and Jim Hester and Max Kuhn and Thomas Lin Pedersen and Evan Miller and Stephan Milton Bache and Kirill Müller and Jeroen Ooms and David Robinson and Dana Paige Seidel and Vitalie Spinu and Kohske Takahashi and Davis Vaughan and Claus Wilke and Kara Woo and Hiroaki Yutani}, 62 | year = {2019}, 63 | journal = {Journal of Open Source Software}, 64 | volume = {4}, 65 | number = {43}, 66 | pages = {1686}, 67 | } 68 | 69 | 70 | @InCollection{knitr2014, 71 | booktitle = {Implementing Reproducible Computational Research}, 72 | editor = {Victoria Stodden and Friedrich Leisch and Roger D. Peng}, 73 | title = {knitr: A Comprehensive Tool for Reproducible Research in {R}}, 74 | author = {Yihui Xie}, 75 | publisher = {Chapman and Hall/CRC}, 76 | year = {2014}, 77 | note = {ISBN 978-1466561595}, 78 | } 79 | 80 | @Manual{kableExtra2020, 81 | title = {kableExtra: Construct Complex Table with 'kable' and Pipe Syntax}, 82 | author = {Hao Zhu}, 83 | year = {2020}, 84 | note = {R package version 1.2.1}, 85 | url = {https://CRAN.R-project.org/package=kableExtra}, 86 | } 87 | 88 | @Manual{shinystan2018, 89 | title = {shinystan: Interactive Visual and Numerical Diagnostics and Posterior 90 | Analysis for Bayesian Models}, 91 | author = {Jonah Gabry}, 92 | year = {2018}, 93 | note = {R package version 2.5.0}, 94 | url = {https://CRAN.R-project.org/package=shinystan}, 95 | } 96 | 97 | @Article{bayesplot2019, 98 | title = {Visualization in Bayesian workflow}, 99 | author = {Jonah Gabry and Daniel Simpson and Aki Vehtari and Michael Betancourt and Andrew Gelman}, 100 | year = {2019}, 101 | journal = {J. R. Stat. Soc. A}, 102 | volume = {182}, 103 | issue = {2}, 104 | pages = {389-402}, 105 | } 106 | 107 | @article{davidson1977extending, 108 | title={On extending the Bradley-Terry model to incorporate within-pair order effects}, 109 | author={Davidson, Roger R and Beaver, Robert J}, 110 | journal={Biometrics}, 111 | pages={693--702}, 112 | year={1977}, 113 | publisher={JSTOR} 114 | } 115 | 116 | @article{springall1973response, 117 | title={Response Surface Fitting Using a Generalization of the Bradley-Terry Paired Comparison Model}, 118 | author={Springall, A}, 119 | journal={Journal of the Royal Statistical Society: Series C (Applied Statistics)}, 120 | volume={22}, 121 | number={1}, 122 | pages={59--68}, 123 | year={1973}, 124 | publisher={Wiley Online Library} 125 | } 126 | 127 | @article{bockenholt2001hierarchical, 128 | title={Hierarchical modeling of paired comparison data.}, 129 | author={B{\"o}ckenholt, Ulf}, 130 | journal={Psychological Methods}, 131 | volume={6}, 132 | number={1}, 133 | pages={49}, 134 | year={2001}, 135 | publisher={American Psychological Association} 136 | } 137 | 138 | @Manual{cmdstanr, 139 | title = {cmdstanr: R Interface to 'CmdStan'}, 140 | author = {Jonah Gabry and Rok Češnovar}, 141 | year = {2020}, 142 | note = {https://mc-stan.org/cmdstanr, https://discourse.mc-stan.org}, 143 | } 144 | -------------------------------------------------------------------------------- /vignettes/e_contributing.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Contributing guide" 3 | --- 4 | 5 | ```{r, include = FALSE} 6 | knitr::opts_chunk$set( 7 | collapse = TRUE, 8 | comment = "#>" 9 | ) 10 | ``` 11 | 12 | # Contributing 13 | 14 | If you are interested you are welcome to contribute to the repository through pull requests. 15 | 16 | If you have a strong need for a model that is not covered in this package (and do not want to code it yourself) send me an email or a suggestion in Github 17 | 18 | ## Short guide/suggestions 19 | 20 | TODO 21 | The points below are mostly reminders for me but hopefully it can help others 22 | 23 | * Use the `devtools` package to create R files, test files, vignettes etc 24 | * In the R folder: 25 | - `bpc.R`: this contains a single function (rather large) that is the main function call of the package. We currently have only a single Stan model with lots of optional parameters. These are configured in the main bpc function 26 | - `bpc_exports.R`: these are helper functions to handle the `bpc` object and are exposed to the user. Only add here functions that receive a bpc object and that the users will have an interest. 27 | - `bpc_get_probabilities.R` Get probabilities related functions 28 | - `bpc_get_rank_of_players.R` Get rank related functions 29 | - `bpc_get_parameters.R` Get parameters related functions 30 | - `bpc_plot.R` Functions for possible plots 31 | - `bpc_helpers_X.R` additional functions to facilitate and support either the bpc function or the exports functions. None of these functions should be exported, although all of them should be documented. We divided these helpers in several files. the indexes are related to creating and checking names and indexes in lookup tables, the hpd to HPDI related functions, the checks to check correct specification of the data frame columns 32 | - `bpc_s3.R` all functions here are the custom implementation of the generic base functions from R, such as `summary`, `print`, `predict` and `plot` 33 | - Generated quantities is implemented in the `predict` function and in the file `bpc_btpredict.R` 34 | - `data-docs.R` documentation for the data. 35 | - `bpc_object.R` declaration and creation of the bpc class. 36 | 37 | 38 | 39 | 40 | ## Github actions 41 | 42 | Currently we rely on few Github actions from r-lib with some modifications to install `cmdstanr` and `cmdstan.` Basic code comes from the `cmdstanr` actions. 43 | 44 | * R-CMD-check standard. This checks in windows latest, macOS latest, ubuntu latest and ubuntu devel 45 | * pkgdown. The pkg site is build automatically 46 | * test-coverage. Runs covr automatically and upload to codecov 47 | --------------------------------------------------------------------------------