[R] indexing a parameter in nls

John Hendrickx john_hendrickx at yahoo.com
Thu Jul 15 14:02:31 CEST 2004


I'm trying to estimate a "diagonal reference model" in nls. In its
basic form, the model consists of two factors with equal categories
and a dependent variable. The model fits the main effects of the two
factors such that they are proportional to each other. So the model I
want to fit is:

Y=p*m[f1]+(1-p)*m[f2]

The m-parameters indicate the "average" main effect of factors f1 and
f2 (with no intercept). The p-parameter indicates the relative impact
of the two factors on Y. If p=.5, f1 and f2 have equal impact, for
p>.5, f1 has greater impact, for p<.5 f2 has greater impact. 

I can estimate the model as follows for f1="row", f2="col",
Y="nkids":

rd<-model.matrix(~as.factor(row)-1)
cd<-model.matrix(~as.factor(col)-1)

strt<-list(m1=2,m2=2,m3=2,m4=2,m5=3,p=.5)

nls(nkids~p*(rd[,1]*m1+rd[,2]*m2+rd[,3]*m3+rd[,4]*m4+rd[,5]*m5) +
      (1-p)*(cd[,1]*m1+cd[,2]*m2+cd[,3]*m3+cd[,4]*m4+cd[,5]*m5),
      start=strt)
---

What I want however, is a more compact specification, where "row" and
"col" are used to index which m-parameter should be used, something
like:

nls(nkids~p*strt[row]+(1-p)*strt[col],start=strt)

This specification produces an error: "Error in nlsModel(formula, mf,
start) : singular gradient matrix at initial parameter estimates". I
suspect the problem is that "strt[row]" evaluates to a list of
starting values rather than the object names m1, m2, etc. I've tried

nls(nkids~p*get(names(strt[row]))+(1-p)*get(names(strt[col])),start=strt)

But this produces an error as well: "Error in qr.qty(QR, resid) : qr
and y must have the same number of rows"

Can anyone point me in the right direction? How can I use "row" and
"col" to index the appropriate "m" parameter in this model?




More information about the R-help mailing list