I’ve been brushing up on my statistics lately, and realizing it’s kind of important to include on my graphs with linear models the linear equation, p-value and r^2-value

I’ve been brushing up on my statistics lately, and realizing it’s kind of important to include on my graphs with linear models the linear equation, p-value and r^2-value. I was hoping to find a simple R library to produce this text, but I ended up using a StackOverflow answer, along with some R code I wrote myself

get_equation <- function(model) {
  pValue <- paste('p_value=',  format(summary(model)$coefficients[,4], scientific=T), sep='')  
  r2Value <- paste('r^2_value=', round(summary(model)$r.squared,4), sep='') 
  
  formula <- broom::tidy(model)[, 1:2] %>%
    mutate(sign = ifelse(sign(estimate) == 1, '+', '-')) %>% #coeff signs
    mutate_if(is.numeric, ~ abs(round(., 7))) %>% #for improving formatting
    mutate(a = ifelse(term == '(Intercept)', paste0('y=', estimate), paste0(sign, estimate, 'x'))) %>%
    summarise(formula = paste(a, collapse = '')) %>%
    as.character 
  
  paste(formula, pValue, r2Value, sep='n')
}

To add this code to the upper right-hand side of the graph, you can use this code in ggplot2:

annotate('text', x=Inf,y=Inf, hjust=1, vjust=1, label=get_formula(lm(comb$avg ~ comb$Value))[1] ) 

Here is a rather silly example:

Leave a Reply

Your email address will not be published. Required fields are marked *