Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

dev #243

Merged
merged 4 commits into from
Oct 18, 2019
Merged

dev #243

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: bayestestR
Type: Package
Title: Understand and Describe Bayesian Models and Posterior Distributions
Version: 0.3.3
Version: 0.4.0
Authors@R: c(
person("Dominique",
"Makowski",
Expand Down Expand Up @@ -52,7 +52,7 @@ LazyData: true
Depends:
R (>= 3.0)
Imports:
insight (>= 0.5.0),
insight (>= 0.6.0),
methods,
stats,
utils
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,11 @@ export(bayesfactor_models)
export(bayesfactor_parameters)
export(bayesfactor_restricted)
export(bayesfactor_savagedickey)
export(bayesian_as_frequentist)
export(check_prior)
export(ci)
export(contr.bayes)
export(convert_bayesian_as_frequentist)
export(convert_p_to_pd)
export(convert_pd_to_p)
export(density_at)
Expand Down Expand Up @@ -267,7 +269,9 @@ importFrom(stats,cor)
importFrom(stats,cor.test)
importFrom(stats,density)
importFrom(stats,getCall)
importFrom(stats,glm)
importFrom(stats,integrate)
importFrom(stats,lm)
importFrom(stats,mad)
importFrom(stats,median)
importFrom(stats,na.omit)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## New functions / features

- `convert_bayesian_to_frequentist` Convert (refit) Bayesian model as frequentist
- `distribution_binomial()` for perfect binomial distributions
- `simulate_ttest()` Simulate data with a mean difference
- `simulate_correlation()` Simulate correlated datasets
Expand Down
71 changes: 71 additions & 0 deletions R/convert_bayesian_to_frequentist.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#' Convert (refit) a Bayesian model to frequentist
#'
#' Refit Bayesian model as frequentist. Can be useful for comparisons.
#'
#' @param model A Bayesian model.
#' @param data Data used by the model. If \code{NULL}, will try to extract it from the model.
#' @examples
#' \donttest{
#' # Rstanarm ----------------------
#' library(rstanarm)
#'
#' # Simple regressions
#' model <- stan_glm(Sepal.Length ~ Petal.Length * Species,
#' data = iris, chains = 2, refresh = 0)
#' bayesian_as_frequentist(model)
#'
#' model <- stan_glm(vs ~ mpg, family = "binomial",
#' data = mtcars, chains = 2, refresh = 0)
#' bayesian_as_frequentist(model)
#'
#' # Mixed models
#' model <- stan_glmer(Sepal.Length ~ Petal.Length + (1|Species),
#' data = iris, chains = 2, refresh = 0)
#' bayesian_as_frequentist(model)
#'
#' model <- stan_glmer(vs ~ mpg + (1|cyl), family = "binomial",
#' data = mtcars, chains = 2, refresh = 0)
#' bayesian_as_frequentist(model)
#' }
#'
#' @importFrom stats lm glm
#' @export
convert_bayesian_as_frequentist <- function(model, data = NULL) {

if (is.null(data)) {
data <- insight::get_data(model)
}

# info
info <- insight::model_info(model)

# Call
called <- model$call
# fun <- as.character(called)[1]
formula <- called$formula
family <- called$family

if (info$is_mixed) {
if (!requireNamespace("lme4", quietly = TRUE)) {
stop("Package 'lme4' required for this function to work. Please install it by running `install.packages('lme4')`.")
}
if (info$is_linear) {
freq <- lme4::lmer(formula, data = data)
} else{
freq <- lme4::glmer(formula, data = data, family = family)
}
} else{
if (info$is_linear) {
freq <- stats::lm(formula, data = data)
} else{
freq <- stats::glm(formula, data = data, family = family)
}
}

freq
}


#' @rdname convert_bayesian_as_frequentist
#' @export
bayesian_as_frequentist <- convert_bayesian_as_frequentist
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ reference:
- title: "Utilities"
contents:
- simulate_correlation
- convert_bayesian_as_frequentist
- contr.bayes
- as.numeric.p_direction
- as.data.frame.density
Expand Down
2 changes: 1 addition & 1 deletion docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/CODE_OF_CONDUCT.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/CONTRIBUTING.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/PULL_REQUEST_TEMPLATE.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/bayes_factors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/bayestestR.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/credible_interval.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/example1.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/example2.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/example3.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/guidelines.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/indicesEstimationComparison.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/indicesExistenceComparison.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/probability_of_direction.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/region_of_practical_equivalence.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/authors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/area_under_curve.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/as.data.frame.density.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/as.numeric.p_direction.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/bayesfactor.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/bayesfactor_inclusion.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/bayesfactor_models.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/bayesfactor_parameters.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/bayesfactor_restricted.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/check_prior.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading