Dynamic formula creation in R?
是否可以将lm()函数与矩阵一起使用?也许正确的问题是:"是否可以在R中动态创建公式?"
我正在创建一个函数,其输出为矩阵,矩阵中的列数不固定=它取决于用户的输入。我想使用矩阵中的数据拟合OLS模型。
-第一列代表因变量
-其他列是自变量。
使用
除了使用OLS公式手动估算方程式以外,还有其他解决方案吗?
可复制的示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | > # When user 1 uses the function, he obtains m1 > m1 <- replicate(5, rnorm(50)) > colnames(m1) <- c("dep", paste0("ind", 1:(ncol(m1)-1))) > head(m1) dep ind1 ind2 ind3 ind4 [1,] 0.5848705 0.3602760 -0.95493403 -1.7278030 -0.1914170 [2,] 1.7167604 -0.1035825 0.31026183 -1.5071415 -1.2748600 [3,] -0.1326187 -0.5669026 0.01819749 0.8346880 -0.6304498 [4,] -0.7381232 0.4612792 -0.36132404 -0.1183131 -0.7446985 [5,] 0.9919123 -1.3228248 -0.44728270 0.6571244 -0.4895385 [6,] -0.8010111 0.8307584 -0.16106804 0.3069870 -0.3834583 > > # When user 2 uses the function, he obtains m2 > m2 <- replicate(6, rnorm(50)) > colnames(m2) <- c("dep", paste0("ind", 1:(ncol(m2)-1))) > head(m2) dep ind1 ind2 ind3 ind4 ind5 [1,] 1.2936031 -0.8060085 0.5020699 -1.699123234 1.0205626 1.0787888 [2,] 1.2357370 0.5973699 -1.2134283 -0.928040354 -0.3037920 -0.1251678 [3,] 0.5292583 0.1063213 -1.3036526 0.395886937 -0.1280863 1.1423532 [4,] 0.9234484 -0.4505604 1.2796922 0.424705893 -0.5547274 -0.3794037 [5,] -0.8016376 1.1362677 -1.1935238 -0.004460092 -1.4449704 -0.3739311 [6,] 0.4385867 0.5671138 0.4493617 -2.277925642 -0.8626944 -0.6880523 |
用户1将通过以下方式估算线性模型:
1 | lm(dep ~ ind1 + ind2 + ind3 + ind4, data = m1) |
同时用户2具有一个额外的自变量,并且将通过以下方式估算线性模型:
1 | lm(dep ~ ind1 + ind2 + ind3 + ind4 + ind5, data = m1) |
再一次,我有什么办法可以动态创建公式?
是的,实际上,列数越多,公式接口就会遇到性能问题。
因此,实际上,对于较大的列宽,首选矩阵接口。
Is there any way I can create the formula dynamically?
当然,您可以直接通过列索引的向量查找矩阵列,或者通过将名称向量转换为列索引来间接查找矩阵列
使用
但是在您的情况下,您不必担心grep,因为您已经有一个简单的列命名方案,您知道
1 2 3 4 | lm(m1[,'dep'] ~ m1[,2:5]) # or in general lm(m1[,'dep'] ~ m1[,colIndicesVector]) # e.g. c(1,3,4) |