Posts

Showing posts from December, 2019

Scientific plots using ggplot2 in R

Image
Below you can find the R code to create a publication-ready plot. You can change the details of the theme() functions to tail the plot to your needs. # Loading required libraries library(ggplot2) library(grid) # Load the function theme_science1 <- function(base_size = 18,                            facet_border_col = "white",                            legend_justification = c(0,1),                            legend_position = c(0,1)){   theme_bw(base_size = base_size) +   theme(axis.line = element_line(size = 0.7, color = 'black'),             panel.border = element_blank(),             panel.grid.major = element_line(size = 0.1, color = "grey90", linetype = "dotted"),       ...

A simple function to create a regression output in R

Image
There are several ways to create an output of regression analysis in R. This is a simple custom made function. # Clear all rm(list=ls()) # Libraries library(QuantPsyc) # Regression summary function table_lm <- function(lm_object, digit_rounding){   summary_lmobject = summary(lm_object)   df_output = data.frame(   predictors = names(lm_object$coefficients),   beta = round(as.numeric(summary_lmobject$coefficients[,1]), digit_rounding),   SE = round(as.numeric(summary_lmobject$coefficients[,2]), digit_rounding),   SE95ci_low = round(as.numeric(confint(lm_object, level = 0.95)[,1]),digit_rounding),   SE95ci_high = round(as.numeric(confint(lm_object, level = 0.95)[,2]),digit_rounding),   stdbeta = c(NA,round(lm.beta(lm_object), digit_rounding)),   ttest = round(summary_lmobject$coefficients[,3], digit_rounding),   pvalues = sapply(as.numeric(summary_lmobject$coefficients[,4]),             ...

Plotting pre-post results in R

Image
A quick code to create a useful plot to visualise pre- and post-test results. I was inspired by this  post , this  post , and this  post . A scatterplot between pre-test and post-test scores for the control and the training group is presented. The straight black lines represent correlations between pre-test and post-test scores within the two groups. The straight lightgrey line represents the same values at pre-test and post-test –> Subjects above the line display a better performance at post-test compared to pre-test. At the margins, the boxplots are reported as well as the mean (i.e., diamond) and 95% confidence intervals (i.e., error bars). In the case of the post-test scores (plot on the right margin), the adjusted means (i.e., black triangles) and associated 95% confidence intervals (i.e., error bars) are reported. The table reports the results for the ANCOVA and its assumptions. In this example, participants in the training group show an improvement in a Working ...

Reporting Bayes Factor in regression analysis

A quick function to extract and report large Bayes factors as a power of 10 when running a regression analysis. The function takes the output of regressionBF (Package BayesFactor: Morey & Rouder, 2018), extracts the BF and converts into a format for Rmarkdown. # A function to extract and report Bayes Factors BF_scirep_str <- function(model) { output <- formatC(round(extractBF(model, onlybf = T)), digits = 3) output <- sub("e", "x10<sup>", output) #Replace e with 10^ output <- sub("\\+0?", "", output) #Remove + symbol and leading zeros on expoent, if > 1 output <- sub("-0?", "-", output) #Leaves - symbol but removes leading zeros on expoent, if < 1 output } An example: # Library library(BayesFactor) # Regression model model <- regressionBF(mpg ~ hp + wt, data = mtcars) model ## Bayes factor analysis ## -------------- ## [1] hp : 56963.84 ±0.01% ## [2] wt : 45657981 ±...

Quartiles boxplot in R

Image
Boxplots are one of the best ways to represent distributions. Below, there is some code to create a custom boxplot by determining the percentiles of the distribution that the whiskers and the box represent. In the example, I plotted the quartiles of the distribution of weight as a function of the group from the dataset  PlantGrowth . First, we create a function to extract quartiles. The values in  probs  represent the percentiles of the distributions, which then will be plotted. Then, we examine the quartiles of the distribution of weight for each group (i.e., ctrl, trt1, trt2). # Create the function quartiles quartiles <- function(x) { r <- quantile(x, probs=c(0, 0.25, 0.5, 0.75, 1)) names(r) <- c("ymin", "lower", "middle", "upper", "ymax") r } # Quartiles of weight per group by(PlantGrowth$weight, INDICES = PlantGrowth$group, FUN = quartiles) ## PlantGrowth$group: ctrl ## ymin lower middle upper ymax ## 4.1...