Skip to content

Commit

Permalink
README + report_priors()
Browse files Browse the repository at this point in the history
  • Loading branch information
DominiqueMakowski committed Oct 29, 2020
1 parent 7c948b1 commit 02223d2
Show file tree
Hide file tree
Showing 10 changed files with 557 additions and 117 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Collate:
'report_parameters.R'
'report_participants.R'
'report_performance.R'
'report_priors.R'
'report_random.R'
'report_sample.R'
'report_statistics.R'
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ S3method(print,report_intercept)
S3method(print,report_model)
S3method(print,report_parameters)
S3method(print,report_performance)
S3method(print,report_priors)
S3method(print,report_random)
S3method(print,report_statistics)
S3method(print,report_table)
Expand Down Expand Up @@ -117,6 +118,8 @@ S3method(report_performance,merMod)
S3method(report_performance,stanreg)
S3method(report_performance,survreg)
S3method(report_performance,zeroinfl)
S3method(report_priors,default)
S3method(report_priors,stanreg)
S3method(report_random,MixMod)
S3method(report_random,default)
S3method(report_random,glmmTMB)
Expand Down Expand Up @@ -190,6 +193,7 @@ S3method(summary,report_intercept)
S3method(summary,report_model)
S3method(summary,report_parameters)
S3method(summary,report_performance)
S3method(summary,report_priors)
S3method(summary,report_random)
S3method(summary,report_statistics)
S3method(summary,report_table)
Expand All @@ -201,6 +205,7 @@ export(as.report_intercept)
export(as.report_model)
export(as.report_parameters)
export(as.report_performance)
export(as.report_priors)
export(as.report_random)
export(as.report_statistics)
export(as.report_table)
Expand Down Expand Up @@ -229,6 +234,7 @@ export(report_packages)
export(report_parameters)
export(report_participants)
export(report_performance)
export(report_priors)
export(report_random)
export(report_sample)
export(report_statistics)
Expand Down
8 changes: 8 additions & 0 deletions R/report.lm.R
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,14 @@ report_model.lm <- function(x, table=NULL, ...) {
text <- paste0(text, ". ", summary(random_text))
}

# Bayesian
if(info$is_bayesian){
priors_text <- report_priors(x)
text_full <- paste0(text_full, ". ", as.character(priors_text))
text <- paste0(text, ". ", summary(priors_text))
}


as.report_model(text_full, summary = text, ...)
}

Expand Down
30 changes: 29 additions & 1 deletion R/report.stanreg.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#'
#' # Bayesian models
#' if(require("rstanarm")){
#' model <- stan_glm(mpg ~ cyl + wt, data = mtcars, refresh=0, iter=600)
#' model <- stan_glm(mpg ~ qsec + wt, data = mtcars, refresh=0, iter=600)
#' r <- report(model)
#' r
#' summary(r)
Expand Down Expand Up @@ -52,6 +52,34 @@ report_text.stanreg <- report_text.lm
# ==================== Specific to Bayes ===================================


# report_priors -----------------------------------------------------------

#' @export
report_priors.stanreg <- function(x) {
params <- bayestestR::describe_prior(x)
params <- params[params$Parameter != "(Intercept)", ]

# Return empty if no priors info
if (!"Prior_Distribution" %in% names(params) | nrow(params) == 0) {
return("")
}

values <- ifelse(params$Prior_Distribution == "normal",
paste0("mean = ", insight::format_value(params$Prior_Location), ", SD = ", insight::format_value(params$Prior_Scale)),
paste0("location = ", insight::format_value(params$Prior_Location), ", scale = ", insight::format_value(params$Prior_Scale))
)

values <- paste0(params$Prior_Distribution, " (", values, ")")

if (length(unique(values)) == 1 & nrow(params) > 1) {
text <- paste0("all set as ", values[1])
} else {
text <- paste0("set as ", format_text(values))
}

text <- paste0(" Priors over parameters were ", text, " distributions.")
as.report_priors(text)
}


# report_parameters -------------------------------------------------------
Expand Down
63 changes: 63 additions & 0 deletions R/report_priors.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#' Report priors of Bayesian models
#'
#' Reports priors of Bayesian models (see list of supported objects in \code{\link{report}}).
#'
#' @inheritParams report
#' @inheritParams report_table
#' @inheritParams report_text
#' @inheritParams as.report
#'
#' @return A \code{character} string.
#'
#' @examples
#' library(report)
#'
#' # Bayesian models
#' if(require("rstanarm")){
#' model <- stan_glm(mpg ~ disp, data = mtcars, refresh=0, iter=1000)
#' r <- report_priors(model)
#' r
#' summary(r)
#' }
#' @export
report_priors <- function(x, ...) {
UseMethod("report_priors")
}


#' @export
report_priors.default <- function(x, ...) {
stop(paste0("report_priors() is not available for objects of class ", class(x)))
}

# METHODS -----------------------------------------------------------------


#' @rdname as.report
#' @export
as.report_priors <- function(x, summary = NULL, ...) {
class(x) <- unique(c("report_priors", class(x)))
attributes(x) <- c(attributes(x), list(...))

if (!is.null(summary)) {
attr(x, "summary") <- summary
}
x
}


#' @export
summary.report_priors <- function(object, ...) {
if(is.null(attributes(object)$summary)){
object
} else{
attributes(object)$summary
}
}

#' @export
print.report_priors <- function(x, ...) {
cat(paste0(x, collapse = "\n"))
}


185 changes: 156 additions & 29 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ editor_options:
knitr::opts_chunk$set(
collapse = TRUE,
tidy.opts=list(width.cutoff=60),
tidy=TRUE,
tidy=FALSE,
dpi=450,
fig.path = "man/figures/"
)
Expand Down Expand Up @@ -100,58 +100,185 @@ The `report()` function works on a variety of models, as well as other objects s
report(iris)
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
print(report(iris), width = 120)
print(report(iris), width = 110)
```

These reports nicely work within the [*tidyverse*](https://github.com/tidyverse) workflow:

```{r message=FALSE, warning=FALSE, eval=FALSE}
library(dplyr)
cor.test(iris$Sepal.Length, iris$Petal.Length) %>%
iris %>%
select(-starts_with("Sepal")) %>%
group_by(Species) %>%
report() %>%
summary()
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
iris %>%
select(-starts_with("Sepal")) %>%
group_by(Species) %>%
report() %>%
summary() %>%
print(width=110)
```


### *t*-tests and correlations

Reports can be used to automatically format tests like *t*-tests or correlations.

```{r message=FALSE, warning=FALSE, eval=FALSE}
report(t.test(mtcars$mpg ~ mtcars$am))
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
t.test(mtcars$mpg ~ mtcars$am) %>%
report() %>%
print(width = 110)
```

As mentioned, you can also create tables with the `as.data.frame()` functions, like for example with this correlation test:

```{r message=FALSE, warning=FALSE}
cor.test(iris$Sepal.Length, iris$Sepal.Width) %>%
report() %>%
as.data.frame()
```



### ANOVAs

This works great with ANOVAs, as it includes **effect sizes** and their interpretation.

```{r message=FALSE, warning=FALSE, eval=FALSE}
aov(Sepal.Length ~ Species, data=iris) %>%
report()
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
print(report(cor.test(iris$Sepal.Length, iris$Petal.Length)), width = 120)
aov(Sepal.Length ~ Species, data=iris) %>%
report() %>%
print(width = 110)
```

<!-- You can also create tables with the `table_short()` and `table_long()` functions: -->
### General Linear Models (GLMs)

Reports are also compatible with GLMs, such as this **logistic regression**:

```{r message=FALSE, warning=FALSE, eval=FALSE}
model <- glm(vs ~ mpg * drat, data=mtcars, family="binomial")
report(model)
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
glm(vs ~ mpg * drat, data=mtcars, family="binomial") %>%
report() %>%
print(width = 110)
```

<!-- ```{r message=FALSE, warning=FALSE} -->
<!-- lm(Sepal.Length ~ Petal.Length + Species, data = iris) %>% -->
<!-- report() %>% -->
<!-- table_short() -->
<!-- ``` -->
### Mixed Models

Mixed models (coming from example from the `lme4` package), which popularity and usage is exploding, can also be reported as it should:

```{r message=FALSE, warning=FALSE, eval=FALSE}
library(lme4)
<!-- ### *t*-tests and correlations -->
model <- lme4::lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris)
<!-- ```{r message=FALSE, warning=FALSE, eval=FALSE} -->
<!-- t.test(mtcars$mpg ~ mtcars$am) %>% -->
<!-- report() -->
<!-- ``` -->
<!-- ```{r echo=FALSE, message=FALSE, warning=FALSE} -->
<!-- t.test(mtcars$mpg ~ mtcars$am) %>% -->
<!-- report() %>% -->
<!-- text_long() %>% -->
<!-- print(width = 100) -->
<!-- ``` -->
report(model)
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(lme4)
lme4::lmer(Sepal.Length ~ Petal.Length + (1 | Species), data = iris) %>%
report() %>%
print(width = 110)
```

<!-- ### Report participants details -->

<!-- This can be useful to complete the **Participants** paragraph of your manuscript. -->
### Bayesian Models

<!-- ```{r message=FALSE, warning=FALSE} -->
<!-- data <- data.frame("Age" = c(22, 23, 54, 21), -->
<!-- "Sex" = c("F", "F", "M", "M")) -->
Bayesian models can also be reported using the new [**SEXIT**](https://easystats.github.io/bayestestR/reference/sexit.html) framework, that combines clarity, precision and usefulness.

<!-- paste(report_participants(data, spell_n = TRUE), -->
<!-- "were recruited in the study by means of torture and coercion.") -->
<!-- ``` -->
```{r message=FALSE, warning=FALSE, eval=FALSE}
library(rstanarm)
model <- stan_glm(mpg ~ qsec + wt, data = mtcars)
report(model)
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(rstanarm)
model <- stan_glm(mpg ~ qsec + wt, data = mtcars, refresh=0, iter=1000) %>%
report() %>%
print(width = 110)
```

## Other types of reports

### Specific parts

One can, for complex reports, directly access the pieces of the reports:

```{r message=FALSE, warning=FALSE, eval=FALSE}
model <- lm(Sepal.Length ~ Species, data = iris)
report_model(model)
report_performance(model)
report_statistics(model)
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
model <- lm(Sepal.Length ~ Species, data = iris)
print(report_model(model), width=110)
print(report_performance(model), width=110)
print(report_statistics(model), width=110)
```


### Report participants details

This can be useful to complete the **Participants** paragraph of your manuscript.

```{r message=FALSE, warning=FALSE, eval=FALSE}
data <- data.frame("Age" = c(22, 23, 54, 21),
"Sex" = c("F", "F", "M", "M"))
paste(
report_participants(data, spell_n = TRUE),
"were recruited in the study by means of torture and coercion."
)
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
data <- data.frame("Age" = c(22, 23, 54, 21),
"Sex" = c("F", "F", "M", "M"))
paste(report_participants(data, spell_n = TRUE),
"were recruited in the study by means of torture and coercion.") %>%
print(width=110)
```


### Report sample

Report can also help you create sample description table (also referred to as **Table 1**).

```{r message=FALSE, warning=FALSE}
report_sample(iris, group_by = "Species")
```

### Report system and packages

Finally, **report** includes some functions to help you write the data analysis paragraph about the tools used.

```{r message=FALSE, warning=FALSE, eval=FALSE}
report(sessionInfo())
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
report(sessionInfo()) %>%
print(width=110)
```

## Credits

Expand Down
Loading

0 comments on commit 02223d2

Please sign in to comment.