---
title: "BayesTestR"
output: html_notebook
---
```{r}
library(rstanarm)
library(bayestestR)
library(insight)
```
```{r}
model <- lm(Sepal.Length ~ Petal.Length, data=iris)
summary(model)
```
```{r}
library(ggplot2) # Load the package
# The ggplot function takes the data as argument, and then the variables
# related to aesthetic features such as the x and y axes.
ggplot(iris, aes(x=Petal.Length, y=Sepal.Length)) +
geom_point() + # This adds the points
geom_smooth(method="lm") # This adds a regression line
```
```{r}
model <- stan_glm(Sepal.Length ~ Petal.Length, data=iris)
describe_posterior(model)
```
```{r}
posteriors <- insight::get_parameters(model)
head(posteriors) # Show the first 6 rows
```
```{r}
ggplot(posteriors, aes(x = Petal.Length)) +
geom_density(fill = "orange")
mean(posteriors$Petal.Length)
median(posteriors$Petal.Length)
map_estimate(posteriors$Petal.Length)
```
```{r}
ggplot(posteriors, aes(x = Petal.Length)) +
geom_density(fill = "orange") +
# The mean in blue
geom_vline(xintercept=mean(posteriors$Petal.Length), color="blue", size=1) +
# The median in red
geom_vline(xintercept=median(posteriors$Petal.Length), color="red", size=1) +
# The MAP in purple
geom_vline(xintercept=map_estimate(posteriors$Petal.Length), color="purple", size=1)
```
```{r}
hdi(posteriors$Petal.Length, ci=0.89)
```
```{r}
describe_posterior(model, test = c("p_direction", "rope", "bayesfactor"))
```
# Correlations
```{r}
result <- cor.test(iris$Sepal.Width, iris$Sepal.Length)
result
```
```{r}
library(BayesFactor)
result <- correlationBF(iris$Sepal.Width, iris$Sepal.Length)
```
```{r}
describe_posterior(result)
```
```{r}
bayesfactor(result)
```
```{r}
library(see)
plot(bayesfactor(result)) +
scale_fill_pizza()
```
# T-tests
```{r}
library(dplyr)
library(ggplot2)
# Select only two relevant species
data <- iris %>%
filter(Species != "setosa") %>%
droplevels()
# Visualise distributions and observations
data %>%
ggplot(aes(x = Species, y = Sepal.Width, fill = Species)) +
geom_violindot(fill_dots = "black", size_dots = 1) +
scale_fill_material() +
theme_modern()
```
```{r}
result <- BayesFactor::ttestBF(formula = Sepal.Width ~ Species, data = data)
describe_posterior(result)
```
```{r}
library(see)
plot(bayesfactor(result)) +
scale_fill_pizza()
```