0

I am trying to implement a new nonlinear function to use in nlmer function in lme4 package. But I'm not sure what the problem is. This is the first time I'm trying to use nlmer but I'm following all the instructions I've found on the internet. The first error is about my dataframe.

  data <- read.csv(paste("C:/Users/oguz/Desktop/Runs4SiteModels/db/", "DB4NLSiteModel", Periods[i],".txt", sep=""), sep = "", header = TRUE)
  
  psa_rock <- data$PSAr
  
  nparams <- c("c")
  nonl_fn <- deriv(~ log(( psa_rock + c)/c),
               namevec = c("c"),
               function.arg=c("c", psa_rock))
  
  fm <- nlmer(log(data$PSAm) ~ nonl_fn(c, psa_rock) ~ 1 + data$M1 + data$M3 + data$M85 + data$Nflag + data$Rflag + data$FDepth + 
              data$Dist1 + data$Dist3 + data$VN + (exp(-1*exp(2*log(data$Vs)- 11)) *  log((data$PSAr + c) / c) ) +
              (1|data$EQID) + (1|data$STID), data=data, start=c(c=0.1))

When I run this code, I'm getting the following error:

Error in model.frame.default(data = data, drop.unused.levels = TRUE, formula = log(data$PSAm) ~  : 
  invalid type (list) for variable 'data'

which I wasn't getting it while using lmer function (of course without the nonlinear function). That's why I'm thinking my problem is not about my dataframe.

Other issue that I couldn't stop thinking about, the part in the fixed-effects:

(exp(-1*exp(2*log(data$Vs)- 11)) *  log((data$PSAr + c) / c) )

as you can see my nonlinear function also takes a part in my fixed-effects formula and I'm not quite sure how to implement that. I hope my way is correct but because of my first problem, I couldn't find an opportunity to test that.

  • 2
    Never, ever use `$` in model formulas. `nlmer` knows where to look for variables because you told it by supplying a data.frame to its `data` parameter. `lm` could deal with `data$M1` in a formula but lme4 doesn't support that (because it would make the source code much more complex). – Roland Oct 16 '20 at 10:49
  • You also should avoid referring to variables from the global environment in the model formula. Use `PSAr` instead of `psa_rock`. – Roland Oct 16 '20 at 10:51
  • So, the formula should look like `log(PSAm) ~ nonl_fn(c, PSAr) ~ ...`. – Roland Oct 16 '20 at 10:53

0 Answers0