# In a FRESH session:
# 1.
install.packages("brms")
# 2.
library(brms)
# 3.
brm(mpg ~ wt, data=mtcars)
3. On Bootstrapping
brms
pnorm()
, qnorm()
, pt()
, dt()
, etc.).x <- c(3, 4, 5, 6, 3, 2) # Any vector of values
y <- c(3, 3, 4, 5, 4, 3) # Another vector of values
# Calculate Pearson's correlation coefficient using the formula
n <- length(x)
x_bar <- mean(x)
y_bar <- mean(y)
r <- sum((x - x_bar) * (y - y_bar)) / sqrt(sum((x - x_bar)^2) * sum((y - y_bar)^2))
r
[1] 0.7765803
{correlation}
package{correlation}
is part of the easystats
collection of packages, and allows to run various types of correlations, and to plot the results.cor.test()
, cor_test()
takes the data as a first argument and the variable names next.cor_test()
function returns a data.frame, so it’s easy to extract valuesr_values <- c() # Initialize an empty vector of r values
for (i in 1:5000) { # Repeat the process 5000 times
new_sample <- mtcars[sample(1:nrow(mtcars), replace = TRUE), ] # Sample new data
result <- correlation::cor_test(new_sample, "mpg", "qsec") # Compute the correlation
r_values <- c(r_values, result$r) # Append the r value to the vector
}
r_values <- c() # Initialize an empty vector of r values
for (i in 1:5000) { # Repeat the process 5000 times
new_sample <- mtcars[sample(1:nrow(mtcars), replace = TRUE), ] # Sample new data
result <- correlation::cor_test(new_sample, "mpg", "qsec") # Compute the correlation
r_values <- c(r_values, result$r) # Append the r value to the vector
}
data.frame(r = r_values)
to create a dataframe with the vector of r values (that you can pass to ggplot()
)mean(c(0, 0, 1, 4, 6))
){bayestestR}
package (in easystats)Warning
The map_estimate()
function does not return a numeric value. The MAP value can be extracted using as.numeric()
.
data <- data.frame(x = bayestestR::distribution_beta(10000, 1.5, 6))
indices <- data.frame(Index = c("Mean", "Median", "MAP"),
Value = c(mean(data$x), median(data$x), as.numeric(bayestestR::map_estimate(data$x))))
data |>
ggplot(aes(x)) +
geom_histogram(bins=60, color="darkgrey", fill="grey") +
geom_vline(data = indices, aes(xintercept = Value, color = Index), size = 1) +
scale_color_manual(values = c("purple", "green", "blue")) +
theme_bw()
bayestestR::eti()
functiondata <- data.frame(x = bayestestR::distribution_beta(10000, 3, 20))
ci_eti <- bayestestR::eti(data)
bayestestR::estimate_density(data$x, method="logspline") |>
mutate(ETI = ifelse(x > ci_eti$CI_high, "Higher", ifelse(x < ci_eti$CI_low, "Lower", "Within CI"))) |>
ggplot(aes(x=x, y=y)) +
geom_area(aes(fill = ETI)) +
scale_fill_manual(values = c("Lower"="#2196F3", "Higher"="#03A9F4", "Within CI"="#F44336")) +
labs(fill = "95% ETI", x="Bootstrapped distribution of r coefficients", y="Probability") +
theme_bw()
ci_hdi <- bayestestR::hdi(data)
bayestestR::estimate_density(data$x, method="logspline") |>
mutate(HDI = ifelse(x > ci_hdi$CI_high, "Higher", ifelse(x < ci_hdi$CI_low, "Lower", "Within CI"))) |>
ggplot(aes(x=x, y=y)) +
geom_area(aes(fill = HDI)) +
geom_hline(yintercept = 0.95, color="black", linetype="dashed") +
scale_fill_manual(values = c("Lower"="#4CAF50", "Higher"="#8BC34A", "Within CI"="#F44336")) +
labs(fill = "95% HDI", x="Bootstrapped distribution of r coefficients", y="Probability") +
theme_bw()
bayestestR
exists to convert between the two indices.
bayestestR::rope()
function[-0.24, 0.24]
as the ROPE ranger_values <- c() # Initialize an empty vector of r values
for (i in 1:5000) { # Repeat the process 5000 times
result <- mtcars[sample(1:nrow(mtcars), replace = TRUE), ] |> # Sample new data
correlation::cor_test("mpg", "qsec") # Compute the correlation
r_values <- c(r_values, result$r) # Append the r value to the vector
}
[-0.1, 0.1]
0.05
), we can conclude that the effect is not negligible (i.e., “significant”)bayestestR::describe_posterior(r_values,
centrality="mean",
dispersion=TRUE,
ci=0.90,
ci_method="hdi",
rope_range =c(-0.05, 0.05),
test="rope")
Summary of Posterior Distribution
Parameter | Mean | SD | 90% CI | ROPE | % in ROPE
------------------------------------------------------------------
Posterior | 0.42 | 0.11 | [0.25, 0.60] | [-0.05, 0.05] | 0%
Thank you!