Learn R Programming

segmented (version 2.0-0)

segreg: Fitting segmented regression

Description

segreg fits linear or generalized linear segmented regression via a symbolic description of the linear predictor. This is an alternative (introduced since version 2.0-0) to segmented.lm or segmented.glm.

Usage

segreg(formula, data, subset, weights, na.action, family = lm, control = seg.control(), 
      transf = NULL, contrasts = NULL, model = TRUE, x = FALSE, var.psi = TRUE, ...)

Value

An object of class "segmented" which inherits from the class "lm" or "glm" depending on family specification. See segmented.lm.

Arguments

formula

A standard model formula also including one or more 'segmented' terms via the function seg

data

The possible dataframe where the variables are stored

subset

Possible subset, as in lm or glm

weights

Possible weight vector, see weights in lm or glm

na.action

a function which indicates what happen when the data contain NA values. See na.action in lm or glm.

family

The family specification, similar to family in glm. Default to 'lm' for segmented linear models.

control

See seg.control

transf

an optional character string (with "y" as argument) meaning a function to apply to the response variable before fitting

contrasts

see contrasts in glm

model

If TRUE, the model frame is returned.

x

If TRUE, the model matrix is returned.

var.psi

logical, meaning if the standard errors for the breakpoint estimates should be returned in the object fit. If FALSE, the standard errors will be computed by vcov.segmented or summary.segmented. Setting var.psi=FALSE could speed up model estimation for very large datasets. Default to TRUE.

...

Ignored

Author

Vito Muggeo

Warning

Currently for fits returned by segreg, confint.segmented only works if method="delta"

Details

The function allows to fit segmented (G)LM regression models using a formula interface. Results will be the same of those coming from the traditional segmented.lm or segmented.glm, but there are some additional facilities: i) it is possible to constrain some slopes of the segmented relationship, see argument est or R in seg; ii) it is possible to estimate strightforwardly the segmented relationship in each level of a categorical variable, see argument by in seg.

References

Muggeo, V.M.R. (2003) Estimating regression models with unknown break-points. Statistics in Medicine 22, 3055-3071.

See Also

seg

Examples

Run this code
set.seed(10)
x<-1:100
z<-runif(100)
w<-runif(100,-10,-5)
y<-2+1.5*pmax(x-35,0)-1.5*pmax(x-70,0)+10*pmax(z-.5,0)+rnorm(100,0,2)

##the traditional approach
out.lm<-lm(y~x+z+w)
o<-segmented(out.lm, seg.Z=~x+z, psi=list(x=c(30,60),z=.4))

o1<-segreg(y ~ w+seg(x,npsi=2)+seg(z))
all.equal(fitted(o), fitted(o1))

#put some constraints on the slopes
o2<-segreg(y ~ w+seg(x,npsi=2, est=c(0,1,0))+seg(z))
o3<-segreg(y ~ w+seg(x,npsi=2, est=c(0,1,0))+seg(z, est=c(0,1)))

slope(o2)
slope(o3)

##see ?plant for an additional example

Run the code above in your browser using DataLab