Learn R Programming

berryFunctions (version 1.4)

linReg: linear regression with plotting

Description

uses lm; plots data if add=FALSE, draws the regression line with abline and writes the formula with legend

Usage

linReg(x, y, digits=2, inset=0, xpd=par("xpd"), pos1="top", pos2=NULL,
add=FALSE, pch=20, col=2, lwd=3, xlab=deparse(substitute(x)),
ylab=deparse(substitute(y)), ...)

Arguments

x
Numeric. Vector with values of explanatory variable
y
Numeric. Vector with values of dependent variable
digits
Numeric vector of length $\ge 1$. Specifies number of digits a,b,r are rounded to in the formula "y=a*x+b \n R^2=r", respectively. If second and/or third value are not specified, they are set equal to the first. DEFAULT: 2
inset
Numeric vector of length $\le 2$. inset distance(s) from the margins as a fraction of the plot region when formula is placed by keyword. DEFAULT: 0
xpd
Logical, specifying wheter formula can be written only inside the plot region (when FALSE) or inside the figure region including mar (when TRUE) or in the entire device region including oma (when NA). DEFAULT: par("xpd")
pos1
xy.coords-acceptable position of the formula. DEFAULT: "top"
pos2
For numerical coordinates, this is the y-position. DEFAULT: NULL, as in legend
add
Logical. If TRUE, line and text are added to the existing graphic. DEFAULT: FALSE (plots datapoints first and then the line.)
pch
Point Character, see par. DEFAULT: 20
col
Color, see par. DEFAULT: 2
lwd
Numeric. Linewidth, see par. DEFAULT: 3
xlab
Character / Expression. X-axis label if add=FAlSE. DEFAULT: deparse(substitute(x))
ylab
Ditto for Y-Axis.
...
Further arguments passed to plot and abline.

Value

  • None, used for plotting and drawing.

See Also

lm, legend, par, abline.

Examples

Run this code
a <- 1:30
b <- a/2.345+rnorm(30,0,3)

linReg(a,b)
linReg(a,b, ylab="Hallo", pch=1, col=3, main="Regression by Berry")
linReg(a, b, pos1=15, pos2=0) # topleft corner of legend box (not drawn)

# For more flexibility with the datapoints, plot first, then use linReg with add=TRUE:
plot(a,b, xlim=c(-5,45))
linReg(a, b, pos1="bottomright", add=TRUE, inset=.1) # inset: distance from plot border
linReg(a, b, digits=c(7,4,3), add=TRUE, col=3, lty=2, lwd=4)
linReg(a, b, digits=3, pos1="topleft", inset=c(-0.1, 0.3), xpd=TRUE, add=TRUE)

Run the code above in your browser using DataLab