r - What exactly happened when predict() is used in lines()? -


when doing linear modeling, can use predict() within lines() , nice plotting. example

year <- 1:15 sales <- c(301,320,372,423,500,608,721,826,978,1135,1315,1530,1800,2152,2491) yearsales <- data.frame(year,sales) logyearsales.fit <- lm(log(sales)~year) plot(year,log(sales)) lines(year,predict(logyearsales.fit),col="red",lwd=2) 

however, when combine nls() , lines(), odds happen, this:

library(mass) survey1<-survey[!is.na(survey$pulse)&!is.na(survey$height),c("pulse","height")] expn <- function(b0,b1,x){             model.func <- b0 + b1*log(x)             z <- cbind(1,log(x))             dimnames(z) <- list(null, c("b0","b1"))             attr(model.func,"gradient") <- z  model.func } survey1<-as.data.frame(survey1) aa=nls(height~expn(b0,b1,pulse), data=survey1,start=c(b0=180,b1=2),trace=true) plot(survey1) lines(survey1[,1],predict(aa),col="red",lwd=2) 

you can see red curve thick , if contaning many lines. cannot understand this.

the problem pulse values not in order. try

survey1 <- survey1[order(survey1$pulse), ] 

and repeat.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -