Joe Byers
2006-Sep-21 17:50 UTC
[Rd] Help understanding how nls parses the formula argument to estimate the model
I could use some help understanding how nls parses the formula argument to a model.frame and estimates the model. I am trying to utilize the functionality of the nls formula argument to modify garchFit() to handle other variables in the mean equation besides just an arma(u,v) specification. My nonlinear model is y<-nls(t~a*sin(w*2*pi/365*id+p)+b*id+int,data=t1, start=list(w=.5,a=.1,p=.5,b=init.y$coef[2],int=init.y$coef[1] ), control=list(maxiter=1000000,minFactor=1e-18)) where t is change in daily temperatures, id is just a time trend and the a*sin is a one year fourier series. I have tried to debug the nls code using the following code t1<-data.frame(t=as.vector(x),id=index(x)) data=t1; formula <- as.formula(t ~ a *sin(w *2* pi/365 * id + p) + b * id + int); varNames <- all.vars(formula) algorithm<-'default'; mf <- match.call(definition=nls,expand.dots=FALSE, call('nls',formula, data=parent.frame(),start,control = nls.control(), algorithm = "default", trace = FALSE, subset, weights, na.action, model = FALSE, lower = -Inf, upper = Inf)); mWeights<-F;#missing(weights); start=list(w=.5,a=.1,p=.5,b=init.y$coef[2],int=init.y$coef[1] ); pnames <- names(start); varNames <- varNames[is.na(match(varNames, pnames, nomatch = NA))] varIndex <- sapply(varNames, function(varName, data, respLength) { length(eval(as.name(varName), data))%%respLength == 0}, data, length(eval(formula[[2]], data)) ); mf$formula <- as.formula(paste("~", paste(varNames[varIndex], collapse = "+")), env = environment(formula)); mf$start <- NULL;mf$control <- NULL;mf$algorithm <- NULL; mf$trace <- NULL;mf$model <- NULL; mf$lower <- NULL;mf$upper <- NULL; mf[[1]] <- as.name("model.frame"); mf<-evalq(mf,data); n<-nrow(mf) mf<-as.list(mf); wts <- if (!mWeights) model.weights(mf) else rep(1, n) if (any(wts < 0 | is.na(wts))) stop("missing or negative weights not allowed") m <- switch(algorithm, plinear = nlsModel.plinear(formula, mf, start, wts), port = nlsModel(formula, mf, start, wts, upper), nlsModel(formula, mf, start, wts)); I am struggling with the environment issues associated with performing these model.frame operations with the eval functions. thank you