Have a look at optim (which supports a number of different algorithms via
the method= arg) and segmented in package segmented which does segmented
regression.
For example,
ss <- function(par) {
b <- par[1]; c1 <- par[2]; c2 <- par[3]; d <- par[4]
x <- df1$x; y <- df1$y
sum((y - (d+(x-b)*c1*(x-b<0)+(x-b)*c2*(x-b>=0)))^2)
}
optim(sl,ss)
Karl Knoblick <karlknoblich <at> yahoo.de> writes:
:
: Hallo!
:
: I have a problem with fitting data with nls. The first
: example with y1 (data frame df1) shows an error, the
: second works fine.
:
: Is there a possibility to get a fit (e.g. JMP can fit
: also data I can not manage to fit with R). Sometimes I
: also got an error singularity with starting
: parameters.
:
: # x-values
: x<-c(-1,5,8,11,13,15,16,17,18,19,21,22)
: # y1-values (first data set)
: y1=c(-55,-22,-13,-11,-9.7,-1.4,-0.22,5.3,8.5,10,14,20)
: # y2-values (second data set)
: y2=c(-92,-42,-15,1.3,2.7,8.7,9.7,13,11,19,18,22)
:
: # data frames
: df1<-data.frame(x=x, y=y1)
: df2<-data.frame(x=x, y=y2)
:
: # start list for parameters
: sl<-list( d=0, b=10, c1=90, c2=20)
:
: # y1-Analysis - Result: Error in ... singular
: gradient
: nls(y~d+(x-b)*c1*(x-b<0)+(x-b)*c2*(x-b>=0), data=df1,
: start=sl)
: # y2-Analysis - Result: working...
: nls(y~d+(x-b)*c1*(x-b<0)+(x-b)*c2*(x-b>=0), data=df2,
: start=sl)
:
: # plots to look at data
: par(mfrow=c(1,2))
: plot(df1$x,df1$y)
: plot(df2$x,df2$y)
:
: Perhaps there is another fitting routine? Can anybody
: help?
:
: Best wishes,
: Karl
:
:
:
:
:
:
: ___________________________________________________________
: Bestellen Sie Y! DSL und erhalten Sie die AVM "FritzBox SL" f伱伡r 0伮.
: Sie sparen 119伮 und bekommen 2 Monate Grundgeb伱伡hrbefreiung.
:
: ______________________________________________
: R-help <at> stat.math.ethz.ch mailing list
: https://www.stat.math.ethz.ch/mailman/listinfo/r-help
: PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
:
: