lm_eqn: Return a linear model equation with R^2 value

Description Usage Arguments Value Author(s) Examples

Description

Return the linear model equation of y~x formatted for plotting. Takes the first column of the data frame as the dependent variable and the second column as the independent variable.

Usage

1

Arguments

df

Two column data frame, first column is the dependent variable (y) and second column is the independent variable (x)

Value

Returns a latex formatted string with the linear model fit equation and r^2 value.

Author(s)

Greg Ziegler

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
set.seed(1)
x <- rnorm(100)
y <- rnorm(100)
x <- c(-10, x, 10)
y <- c(-20, y, 20)
df <- data.frame(id=sample(LETTERS[1:5],length(x),replace=TRUE),x,y)
lm_eqn(df[,3:2])
###Put it on a plot###
library(ggplot2)
ggplot(df,aes(x=x,y=y)) + geom_point() + geom_smooth(method="lm") +annotate("text", label=lm_eqn(df[,3:2]), parse=TRUE, x=Inf, y=Inf, hjust=1.1, vjust=1.5)

## The function is currently defined as
function (df) 
{
    m = lm(df[, 1] ~ df[, 2])
    paste("italic(y)==", format(coef(m)[1], digits = 2), "+", 
        format(coef(m)[2], digits = 2), "%.%italic(x)*\",\"~~italic(r)^2==", 
        format(summary(m)$r.squared, digits = 3), sep = "")
  }

gziegler/ionomicsUtils documentation built on June 20, 2019, 8:04 p.m.