简体   繁体   English

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

[英]Nonlinear model with many independent variables (fixed effects) in R

I'm trying to fit a nonlinear model with nearly 50 variables (since there are year fixed effects). 我正在尝试使用将近50个变量拟合非线性模型(因为存在固定年限的影响)。 The problem is I have so many variables that I cannot write the complete formula down like 问题是我有太多变量,无法像上面那样写下完整的公式

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))

where y is the binary response variable, year.matirx is a matrix of 45 columns (indicating 45 different years) and x is the independent variable. 其中y是二进制响应变量, year.matirx是45列的矩阵(指示45个不同的年份), x是自变量。 The parameters need to be estimated are t1, t2, ..., t45, g, d . 需要估计的参数是t1, t2, ..., t45, g, d

I have good starting values for t1, ..., t45, g, d . 对于t1, ..., t45, g, d我有很好的起始值。 But I don't want to write a long formula for this nonlinear regression. 但是我不想为这种非线性回归写一个长公式。

I know that if the model is linear, the expression can be simplified using 我知道如果模型是线性的,则表达式可以使用

l_model = lm(y ~ factor(year) + ...)
  1. I tried factor(year) in gnls function but it does not work. 我在gnls函数中尝试了factor(year) ,但它不起作用。
  2. Besides, I also tried 此外,我也尝试过

    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))

It also returns me error message. 它还返回我错误消息。

So, is there any easy way to write down the nonlinear formula and the starting values in R ? 那么,有没有简单的方法可以写下非线性公式和R的起始值?

Since you have not provided any example data, I wrote my own - it is completely meaningless and the model actually doesn't work because it has bad data coverage but it gets the point across: 由于您没有提供任何示例数据,因此我编写了自己的示例数据-完全没有意义,并且该模型实际上不起作用,因为它的数据覆盖率很差,但是很容易理解:

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)

This may not be the most efficient way to do it, but since you can pass a string to as.formula it's pretty easy to use paste commands to construct what you are trying to do. 这可能不是最有效的方法,但是由于您可以将字符串传递给as.formula因此使用paste命令来构造您要尝试的操作非常容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM