Add regression line equation and R^2 on graph

后端 未结 9 2228
梦如初夏
梦如初夏 2020-11-21 07:24

I wonder how to add regression line equation and R^2 on the ggplot. My code is:

library(ggplot2)

df <- data.frame(x = c(1:100))
df$y <- 2         


        
9条回答
  •  青春惊慌失措
    2020-11-21 07:57

    Inspired by the equation style provided in this answer, a more generic approach (more than one predictor + latex output as option) can be:

    print_equation= function(model, latex= FALSE, ...){
        dots <- list(...)
        cc= model$coefficients
        var_sign= as.character(sign(cc[-1]))%>%gsub("1","",.)%>%gsub("-"," - ",.)
        var_sign[var_sign==""]= ' + '
    
        f_args_abs= f_args= dots
        f_args$x= cc
        f_args_abs$x= abs(cc)
        cc_= do.call(format, args= f_args)
        cc_abs= do.call(format, args= f_args_abs)
        pred_vars=
            cc_abs%>%
            paste(., x_vars, sep= star)%>%
            paste(var_sign,.)%>%paste(., collapse= "")
    
        if(latex){
            star= " \\cdot "
            y_var= strsplit(as.character(model$call$formula), "~")[[2]]%>%
                paste0("\\hat{",.,"_{i}}")
            x_vars= names(cc_)[-1]%>%paste0(.,"_{i}")
        }else{
            star= " * "
            y_var= strsplit(as.character(model$call$formula), "~")[[2]]        
            x_vars= names(cc_)[-1]
        }
    
        equ= paste(y_var,"=",cc_[1],pred_vars)
        if(latex){
            equ= paste0(equ," + \\hat{\\varepsilon_{i}} \\quad where \\quad \\varepsilon \\sim \\mathcal{N}(0,",
                        summary(MetamodelKdifEryth)$sigma,")")%>%paste0("$",.,"$")
        }
        cat(equ)
    }
    

    The model argument expects an lm object, the latex argument is a boolean to ask for a simple character or a latex-formated equation, and the ... argument pass its values to the format function.

    I also added an option to output it as latex so you can use this function in a rmarkdown like this:

    
    ```{r echo=FALSE, results='asis'}
    print_equation(model = lm_mod, latex = TRUE)
    ```
    

    Now using it:

    df <- data.frame(x = c(1:100))
    df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
    df$z <- 8 + 3 * df$x + rnorm(100, sd = 40)
    lm_mod= lm(y~x+z, data = df)
    
    print_equation(model = lm_mod, latex = FALSE)
    

    This code yields: y = 11.3382963933174 + 2.5893419 * x + 0.1002227 * z

    And if we ask for a latex equation, rounding the parameters to 3 digits:

    print_equation(model = lm_mod, latex = TRUE, digits= 3)
    

    This yields:

提交回复
热议问题