[R] Plot observed vs. fitted values (weighted nls)

DivineSAAM@aol.com DivineSAAM at aol.com
Thu May 22 03:45:04 CEST 2003


Dear WizaRds,

Given the experimental data,

csdata<-data.frame(
time=c(0,1,3,9,20),
conc=c(638.697,395.69,199.00,141.58,112.16)
)

weighted nls is applied,

wt.MM<- function(resp, time,A1,a1,A2,a2)
{
    pred <- A1*exp(-a1*time)+A2*exp(-a2*time)
    (resp - pred) / sqrt(pred)
}
#
cs.wt <- nls( ~ wt.MM(conc, time,A1,a1,A2,a2), data=csdata,
              start=list(A1=700,a1=1,A2=100,a2=0.1),
             trace = TRUE)
             
x<-csdata$time
y<-csdata$conc

Now, I want a plot of the observed vs. fitted values. I used
# 1. 'seq' to generate series of values for x-axis
# 2. 'predict' to calculate the fitted values
# 3. 'lines' to overlay the smooth curve of the fitted values

smoothx<-seq(0,20,0.1)

smoothy<-predict(cs.wt,list(x=smoothx))


*Unfortunately, this did not work.

My goal was to use

plot(x,y)
lines(smoothx,smoothy)

Got--Error in xy.coords(x, y) : x and y lengths differ--

ANY SUGGESTIONS? PLEASE:-)

I noticed that

> smoothy<-predict(cs.wt,list(x=smoothx))
> smoothy
[1] -0.15787479  0.38479197 -0.43312824  0.29216236 -0.09731213

is not a correct prediction of the response based on the results. 

The following was obtained using S-PLUS (which I really do not want to be using)

> predict(cs.wt)
[1] 640.2268 390.9596 205.7743 136.1417 114.0573


Kindest Regards and Many Thanks in Advance

Oscar A. Linares
The Geriatrics Center
University of Michigan, Ann Arbor




More information about the R-help mailing list