[R] Can I build an array of regrssion model?
Liaw, Andy
andy_liaw at merck.com
Thu Dec 19 00:01:03 CET 2002
And if you *really* want piecewise linear function (and most likely you want
the pieces to be continuous, no?), there are better ways than yours. For
"manual" fitting, use something like:
library(splines)
lm(y ~ bs(x, knots=..., deg=1))
For more automatic fitting, I believe bruto() or even mars() in the package
`mda' will do.
Andy
> -----Original Message-----
> From: Jason Turner [mailto:jasont at indigoindustrial.co.nz]
> Sent: Wednesday, December 18, 2002 5:08 PM
> To: Zhongming Yang
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] Can I build an array of regrssion model?
>
>
> On Wed, Dec 18, 2002 at 03:51:47PM -0500, Zhongming Yang wrote:
> > I am trying to use piecewise linear regression to approximate a
> > nonlinear function.
>
> Why not smooth regression, or non-linear regression?
>
> > Actually, I don't know how many linear functions I
> > need, therefore, I want build an array of regression models
> to automate
> > the approximation job. Could you please give me any clue?
>
> Clue 1) See above. You might be using the wrong tool. A smooth
> regression might be better here. help(loess), library(gss), and
> library(sm) are your friends.
>
> Clue 2) If you really want piecewise linear, a list makes more
> sense than a vector. R does handle vectors quite nicely, but I
> find its real strength is the way it handles complex lists with
> ease.
>
> > Attached is ongoing code:
> >
> > rawData = scan("c:/zyang/mass/data/A01/1.PRN",
> > what=list(numeric(),numeric()));
> > len = length(rawData[[1]]);
> > cuts = len*c(0.01, 0.03, 0.08, 0.18, 0.38, 0.69, 1);
> > cuts = as.integer(cuts);
>
> #change cuts to a matrix of values, col 1 is the lower
> #bound, col 2 is the upper bound for your segments.
>
> cuts <- cbind(c(1,cuts[1:(length(cuts)-1)], cuts)
>
> #make an empty list
> mod.list <- list()
> #fill that list with models
> for(ii in 1:dim(cuts)[1]) {
> start <- cuts[ii,1]
> end <- cuts[ii,2]
> mod.list[[ii]] <- lm(rawData[[2]][start,end] ~
> rawData[[1]][start,end])
> }
>
> #to extract coefficients
> lapply(mod.list,coef)
>
> #to extract coefficients, and confidence intervals
> lapply(mod.list,function(x,...){ coef(summary(x))} )
>
> #to reproduce your ablines
> lapply(mod.list,abline,col="red")
>
> etc
>
> Cheers
>
> Jason
> --
> Indigo Industrial Controls Ltd.
> 64-21-343-545
> jasont at indigoindustrial.co.nz
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
------------------------------------------------------------------------------
More information about the R-help
mailing list