Is there anyway to change any y[i] value (i=2,...6) to make following NLS workable? x <- c(0,5,10,15,20,25,30) y <- c(1.00000,0.82000,0.68000,0.64000,0.66667,0.68667,0.64000) lm(1/y ~~ x) nls(1/y ~~ a+b*x^c, start=list(a=1.16122,b=0.01565,c=1), trace=TRUE) #0.0920573 : 1.16122 0.01565 1.00000 #Error in numericDeriv(form[[3]], names(ind), env) : # Missing value or an infinity produced when evaluating the model --------------------------------- [[alternative HTML version deleted]]
You problem is x^c for x = 0. If you intended only c > 1, try a starting value meeting that condition (but it seems that the optimal c is about 0.27 is you increase x slightly). Why have you used ~~ ? (Maybe because despite being asked not to, you sent HTML mail?) On Tue, 15 Aug 2006, Xiaodong Jin wrote:> Is there anyway to change any y[i] value (i=2,...6) to make following NLS workable? > > x <- c(0,5,10,15,20,25,30) > y <- c(1.00000,0.82000,0.68000,0.64000,0.66667,0.68667,0.64000) > lm(1/y ~~ x) > nls(1/y ~~ a+b*x^c, start=list(a=1.16122,b=0.01565,c=1), trace=TRUE) > > #0.0920573 : 1.16122 0.01565 1.00000 > #Error in numericDeriv(form[[3]], names(ind), env) : > # Missing value or an infinity produced when evaluating the model > > > --------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Hi Why do you want to change your variable values? It smells a rat to me. If you just change your a,b,c values nls arrives to some finite result (e.g. c=1.5 or c=0.3) . BTW by what magic you obtained such precise and wrong estimates for a,b,c? HTH Petr On 15 Aug 2006 at 5:54, Xiaodong Jin wrote: Date sent: Tue, 15 Aug 2006 05:54:51 -0700 (PDT) From: Xiaodong Jin <close2ceo at yahoo.com> To: r-help at stat.math.ethz.ch Subject: [R] nls> Is there anyway to change any y[i] value (i=2,...6) to make > following NLS workable? > > x <- c(0,5,10,15,20,25,30) > y <- c(1.00000,0.82000,0.68000,0.64000,0.66667,0.68667,0.64000) > lm(1/y ~~ x) nls(1/y ~~ a+b*x^c, > start=list(a=1.16122,b=0.01565,c=1), trace=TRUE) > > #0.0920573 : 1.16122 0.01565 1.00000 > #Error in numericDeriv(form[[3]], names(ind), env) : > # Missing value or an infinity produced when evaluating the > # model > > > --------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html and provide commented, > minimal, self-contained, reproducible code.Petr Pikal petr.pikal at precheza.cz
Hello everyone ! I am trying to write a short program to estimate semivariogram parameters. But I keep running into a problem when using the nls function. Could you please shed some light. I have put a sample of one of the codes and ran a short example so you see what I mean. ----------------- fit.gaus<-function(coordinates,values,guess.c0,guess.c1,guess.a) { long<-rep(coordinates[,1],each=length(coordinates[,1])) lag.long<-t(matrix(long,nrow=length(coordinates[,1]),byrow=TRUE)) dif.long <-(lag.long-t(lag.long))^2 lat <-rep(coordinates[,2],each=length(coordinates[,2])) lag.lat<-t(matrix(lat,nrow=length(coordinates[,2]),byrow=TRUE)) dif.lat <-(lag.lat-t(lag.lat))^2 h <-sqrt(dif.long+dif.lat) print (h) #distance matrix between data points if( length(values[1,])>1) { y.m <-apply(values,1,sum,na.rm=TRUE) y.m <-as.matrix(y.m) y.mod <-(1/length(values[1,]))*(y.m) } else { y.mod <-as.matrix(values) } semi <-rep(y.mod,each=length(y.mod)) mat1<-t(matrix(semi,nrow=length(y.mod),byrow=TRUE)) mat2<-t(mat1) semivariance <-(1/2)*(mat1-mat2)^2 model <-semivariance ~c0+c1*(1-exp(-(h^2)/a^2)) parameters <-nls(model,start list(c0=guess.c0,c1=guess.c1,a=guess.a),trace=TRUE) results <-summary(parameters) print(results) } --------> don <-matrix(c(2,3,9,6,5,2,7,9,5,3),5,2) > don[,1] [,2] [1,] 2 2 [2,] 3 7 [3,] 9 9 [4,] 6 5 [5,] 5 3> data <-matrix(c(3,4,2,4,6)) > data[,1] [1,] 3 [2,] 4 [3,] 2 [4,] 4 [5,] 6> fit.gaus(don,data,2,3,5)[,1] [,2] [,3] [,4] [,5] [1,] 0.000000 5.099020 9.899495 5.000000 3.162278 [2,] 5.099020 0.000000 6.324555 3.605551 4.472136 [3,] 9.899495 6.324555 0.000000 5.000000 7.211103 [4,] 5.000000 3.605551 5.000000 0.000000 2.236068 [5,] 3.162278 4.472136 7.211103 2.236068 0.000000 178.9113 : 2 3 5 Error in qr.qty(QR, resid) : 'qr' and 'y' must have the same number of rows>
Hello everyone ! I am trying to write a short program to estimate semivariogram parameters. But I keep running into a problem when using the nls function. Could you please shed some light. I have put a sample of one of the codes and ran a short example so you see what I mean. ----------------- fit.gaus<-function(coordinates,values,guess.c0,guess.c1,guess.a) { long<-rep(coordinates[,1],each=length(coordinates[,1])) lag.long<-t(matrix(long,nrow=length(coordinates[,1]),byrow=TRUE)) dif.long <-(lag.long-t(lag.long))^2 lat <-rep(coordinates[,2],each=length(coordinates[,2])) lag.lat<-t(matrix(lat,nrow=length(coordinates[,2]),byrow=TRUE)) dif.lat <-(lag.lat-t(lag.lat))^2 h <-sqrt(dif.long+dif.lat) if( length(values[1,])>1) { y.m <-apply(values,1,sum,na.rm=TRUE) y.m <-as.matrix(y.m) y.mod <-(1/length(values[1,]))*(y.m) } else { y.mod <-as.matrix(values) } semi <-rep(y.mod,each=length(y.mod)) mat1<-t(matrix(semi,nrow=length(y.mod),byrow=TRUE)) mat2<-t(mat1) semivariance <-(1/2)*(mat1-mat2)^2 model <-semivariance ~c0+c1*(1-exp(-(h^2)/a^2)) parameters <-nls(model,start list(c0=guess.c0,c1=guess.c1,a=guess.a),trace=TRUE) results <-summary(parameters) print(results) } --------------------------> don <-matrix(c(2,3,9,6,5,2,7,9,5,3),5,2) > don[,1] [,2] [1,] 2 2 [2,] 3 7 [3,] 9 9 [4,] 6 5 [5,] 5 3> data <-matrix(c(3,4,2,4,6)) > data[,1] [1,] 3 [2,] 4 [3,] 2 [4,] 4 [5,] 6> fit.gaus(don,data,2,3,5)[,1] [,2] [,3] [,4] [,5] [1,] 0.000000 5.099020 9.899495 5.000000 3.162278 [2,] 5.099020 0.000000 6.324555 3.605551 4.472136 [3,] 9.899495 6.324555 0.000000 5.000000 7.211103 [4,] 5.000000 3.605551 5.000000 0.000000 2.236068 [5,] 3.162278 4.472136 7.211103 2.236068 0.000000 178.9113 : 2 3 5 Error in qr.qty(QR, resid) : 'qr' and 'y' must have the same number of rows>