从线性回归中提取系数

时间:2019-06-20 03:24:13

标签: r regression linear

我想在每次执行线性回归时提取系数“ a”并将其存储在表中

第一个回归将是

 Y = a1X1 + C

其中第一回归将在因变量“ Y”和自变量“ X1”之间。执行回归后,计算出的系数“ a1”将存储在表“ T”中

第二个回归将是

Y = a2X2 + C

其中第二次回归将在因变量“ Y”和自变量“ X2”之间。执行回归后,计算出的系数“ a2”将存储在该表T

继续此过程直到X6,并在表T中获得六个系数值

  Y       X1         X2     X3      X4      X5      X6

0.3236% 0.2561% 0.3302% 0.2800% 0.2886% 0.2363% 0.2755%
0.4547% 0.3860% 0.4673% 0.4626% 0.4407% 0.3966% 0.4460%
0.3820% 0.3193% 0.3882% 0.3910% 0.3333% 0.3307% 0.3485%
0.3951% 0.3190% 0.3991% 0.3506% 0.3594% 0.3230% 0.3692%
0.4460% 0.4047% 0.4566% 0.3841% 0.4125% 0.3561% 0.4319%
0.4525% 0.4163% 0.4629% 0.4142% 0.4000% 0.3871% 0.4357%
0.3680% 0.4011% 0.3759% 0.3890% 0.4193% 0.4802% 0.3490%
0.4304% 0.2657% 0.4224% 0.4619% 0.4936% 0.3776% 0.2827%
0.1360% 0.1866% 0.1351% 0.1694% 0.1853% 0.1316% 0.1649%
0.1317% 0.1335% 0.1276% 0.1682% 0.1960% 0.1318% 0.1356%
0.2713% 0.4491% 0.2891% 0.1901% 0.3513% 0.1816% 0.3869%
0.2404% 0.2389% 0.2371% 0.2217% 0.2162% 0.1827% 0.2571%
0.4934% 0.4529% 0.5047% 0.4766% 0.3890% 0.4124% 0.4610%
0.4083% 0.4513% 0.4128% 0.3612% 0.3974% 0.3759% 0.4667%
0.3033% 0.3063% 0.3058% 0.3342% 0.2688% 0.3286% 0.3019%
0.2976% 0.3226% 0.2967% 0.2697% 0.2626% 0.2860% 0.3172%
0.2505% 0.3238% 0.2554% 0.2682% 0.2495% 0.3014% 0.2931%
0.2077% 0.2491% 0.2019% 0.1866% 0.2063% 0.2065% 0.1928%
0.3669% 0.3316% 0.3703% 0.3034% 0.2806% 0.3556% 0.3310%

到目前为止,我编写的用于清除%符号的代码

图书馆(readxl) Dataset_SR <-read_excel(“ C:/Users/Abhishek/Desktop/Dataset_SR.xlsx”) 查看(Dataset_SR)

打印数据集的结构

str(Dataset_SR)

清除%符号的数据

Dataset_SR [] <-lapply(Dataset_SR,function(x)as.numeric(gsub(“%”,“”,x)))

查看(Dataset_SR)

1 个答案:

答案 0 :(得分:2)

我们可以遍历“ Dataset_SR”的列名,但“ Y”列除外

xcols <- setdiff(names(Dataset_SR), "Y")

或者没有grep

xcols <- grep("^X\\d+$", names(Dataset_SR), value = TRUE)

或使用paste

xcols <- paste0("X", 1:6)

使用lapply,使用reformulate创建公式,在lm中使用该公式,构建模型,然后提取coef不足之处

lapply(xcols, function(nm) 
      coef(lm(reformulate(nm, "Y"), data = Dataset_SR)))

带有内置数据集“ mtcars”的可复制示例

lapply(names(mtcars)[8:11],  function(nm)
         coef(lm(reformulate(nm, "mpg"), data = mtcars)))