2

I have a dependent variable w with independent variable x representing time, which is clustered by variable site. In addition, I have indicator variables for 3 time period: i1, i2, i3, which correspond to three distinct time periods along the timeline (separated by t1 and t2). Since I want an overall fit, which evaluates the trends throughout the three time periods, I am trying to fit a nonlinear function (defined as L3) using slope estimates from as linear model fit like so:

L3<-function(x,alpha1,beta1,beta2,beta3) {
brk = c(t1,t2)
alpha2 <- alpha1 + beta1*brk[1]-beta2*brk[1]
alpha3 = alpha2 + beta2 * brk[2] - beta3*brk[2]
lebrk <- (x < brk[1])
gebrk <- (x > brk[2])
mdbrk <- (x > brk[1] & x < brk[2])

result <- rep(0,length(x))
result[lebrk] <- alpha1 + beta1*x[lebrk]
result[mdbrk] <- alpha2 + beta2*x[mdbrk]
result[gebrk] <- alpha3 + beta3*x[gebrk]
result
}

fit0 = lm(w ~ x + I(i2+i3)+I(x*(i2+i3))+ I(i3)+I(x*i3), data = wr_tx1)
summary(fit0)

alpha1 = fit0$coef[1]
    beta1 = fit0$coef[2]
beta2 = fit0$coef[2] + fit0$coef[4]
beta3 = fit0$coef[2] + fit0$coef[4] + fit0$coef[6]
fit1 <- nls(w ~ L3(x = x, alpha1, beta1, beta2, beta3)
        , data = wr_tx1
        , start = list(alpha1 = alpha1, beta1 = beta1, beta2 = beta2, beta3 = beta3)
        , control=nls.control(maxiter=100))

Since my data is clustered, however, I would like to include random effects at the site level using variable site. I'm trying to figure out how to specify the NLME function to do this, but it seems to require a fixed effects argument even though I'm not concerned with any fixed effects right now. Alternatively, I could include a fixed effect variable for the treatment group that each site is in (variable called tx), but it is unclear how to specific the NLME function in that case either.

To try incorporate the fixed effects, I have tried the following, but I get an error:

wr$tx = as.factor(wr$tx)
fit2 = nlme(w ~ L3(x = x, alpha1, beta1, beta2, beta3)
        , random = alpha1 + beta1 + beta2 + beta3 ~ 1 | site
        , fixed = tx ~ 1
        , data = wr
        , start = list(alpha1 = alpha1, beta1 = beta1, beta2 = beta2, beta3 = beta3))

Error in nlme.formula(w ~ L3(x = x, alpha1, beta1, beta2, beta3), random = alpha1 
  'start' must have a component called 'fixed'

Any help is greatly appreciated.

1 Answers1

3

I happened to get the same error message:
Error in nlme.formula(… : 'start' must have a component called 'fixed'

By looking at the description of the argument ‘start’ in the R help for nlme, I learnt that it is a simple syntax error in the start argument: Type start = c(…,…) instead of start = list(…, …) as in nls, then it should work.

ebcs
  • 31