Description Usage Arguments Value Examples
View source: R/utility_functions.R
Given an indepdent and dependent variable, along with a vector of break points, fits a basic piecewise regression to the data.
1 | simplePiecewiseReg(x, y, breaks, ...)
|
x |
the independent (predictor) variable. |
y |
the dependent variable. |
breaks |
a vector of break points (in the same scale as the independent variable). |
... |
additional parameters for the
|
A list with the output from the
lm
function,
a data frame with the dummy coding used to
construct the piecewise regression, and
x and y-axis values that can be used for
plotting the estimated line segments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | # Define generating parameters
beta = c( 1, 1, 1, -1 )
sigma = .5
# Define break point
breaks = 0
# Simulate data
x = runif( 100, -1, 1 ) # Predictor
# Define design matrix
X = matrix( 1, 100, 2 )
X[ x > breaks, 1] = 0; X[ x <= breaks, 2] = 0
X = cbind( X, X*x )
colnames( X ) = c( 'I1', 'I2', 'S1', 'S2' )
# Generate dependent variable
y = as.vector( X %*% cbind( beta ) )
y = y + rnorm( 100, 0, sigma )
# Fit piecewise regression
fit = simplePiecewiseReg( x, y, breaks )
summary( fit$lm )
Plot results
plot( x, y, pch = 19 )
lines( fit$xa, fit$ya )
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.