R中具有许多自变量(固定效应)的非线性模型

zy.*_*y.G 0 r random-effects non-linear-regression

我正在尝试拟合具有近50 个变量的非线性模型(因为存在年份固定效应)。问题是我有太多的变量,我无法像这样写出完整的公式

nl_exp = as.formula(y ~ t1*year.matrix[,1] + t2*year.matrix[,2] 
                        +... +t45*year.matirx[,45] + g*(x^d))
nl_model =  gnls(nl_exp, start=list(t=0.5, g=0.01, d=0.1))
Run Code Online (Sandbox Code Playgroud)

其中y是二元响应变量,year.matirx是 45 列的矩阵(表示 45 个不同的年份),x是自变量。需要估计的参数是t1, t2, ..., t45, g, d

我有很好的起始值t1, ..., t45, g, d。但是我不想为这个非线性回归写一个很长的公式。

我知道如果模型是线性的,则表达式可以简化为

l_model = lm(y ~ factor(year) + ...)
Run Code Online (Sandbox Code Playgroud)
  1. factor(year)gnls函数中尝试过,但它不起作用。
  2. 此外,我也尝试过

    nl_exp2 = as.formula(y ~ t*year.matrix + g*(x^d))

    nl_model2 = gnls(nl_exp2, start=list(t=rep(0.2, 45), g=0.01, d=0.1))

它还会返回我的错误消息。

那么,有没有简单的方法来写下非线性公式和 中的起始值R

Eum*_*ies 5

由于您没有提供任何示例数据,我自己写了一个 - 它完全没有意义,而且该模型实际上不起作用,因为它的数据覆盖率很差,但它明白了这一点:

y <- 1:100
x <- 1:100
year.matrix <- matrix(runif(4500, 1, 10), ncol = 45)

start.values <- c(rep(0.5, 45), 0.01, 0.1) #you could also use setNames here and do this all in one row but that gets really messy
names(start.values) <- c(paste0("t", 1:45), "g", "d")
start.values <- as.list(start.values)

nl_exp2 <- as.formula(paste0("y ~ ", paste(paste0("t", 1:45, "*year.matrix[,", 1:45, "]"), collapse = " + "), " + g*(x^d)"))

gnls(nl_exp2, start=start.values)
Run Code Online (Sandbox Code Playgroud)

这可能不是最有效的方法,但由于您可以将字符串传递给as.formula它,因此很容易使用paste命令来构建您要执行的操作。