R语言 如何一次运行多个逐步线性回归?

k4ymrczo  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(232)

我试图预测哪些变量会影响lift,即促销食品的销售率。在我的数据集中,lift是因变量,我有八个可能的自变量。Here are the first couple of rows of my dataset.
我需要对30家不同商店的20种不同产品进行此分析。我想知道是否可以在R中同时对所有产品运行20个回归。这样,我只需手动运行30个回归,每个商店一个,并且我将得到每个商店的结果。我希望使用分步回归,因为这是我所熟悉的。
下面是我到目前为止编写的代码,一次只使用一个回归:

data0<- subset(data0, Store == "Store 1")
    data0<- subset(data0, Product == "Product 1")
    
    ########Summary Stats
    head(data0)
    summary(data0)
    str(data0)
    
    ###Data Frame
    data0<-pdata.frame(data0, index=c("Product","Time"))
    data0<-data.frame(data0)

    ###Stepwise
    step_qtr_1v<- lm(Lift ~
              + Depth
             + Length
             + Copromotion
             + Category.Sales.On.Merch
             + Quality.Support.Binary
             
             
             , data = data0)
    summary(step_qtr_1v)

我是R的新手,所以会喜欢简单。谢谢。

fdx2calv

fdx2calv1#

问问题时遵循这些指导方针是非常重要的,尽管如此,我还是用iris数据集做了一个简单的例子。
为了对数据集的不同部分多次运行相同的回归,可以使用lapply()函数,该函数对向量或列表(在本例中为物种名称)应用函数,您只需将其传递给lm()函数中的subset参数:

data("iris")
 
species <- unique(iris$Species)
species

运行species显示此变量的水平:

[1] setosa     versicolor virginica 
Levels: setosa versicolor virginica

运行colnames(iris)会告诉我们要使用哪些变量:

[1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"

之后可以运行lapply函数,如下所示:

models <- lapply(species, function(x) {
   lm(Petal.Length ~ Petal.Width + Sepal.Length + Sepal.Width,
    data = iris, subset = iris$Species == x)
 })
 
lapply(models, summary)

结果是:

[[1]]

Call:
lm(formula = Petal.Length ~ Petal.Width + Sepal.Length + Sepal.Width, 
    data = iris, subset = iris$Species == x)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.38868 -0.07905  0.00632  0.10095  0.48238 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)  
(Intercept)   0.86547    0.34331   2.521   0.0152 *
Petal.Width   0.46253    0.23410   1.976   0.0542 .
Sepal.Length  0.11606    0.10162   1.142   0.2594  
Sepal.Width  -0.02865    0.09334  -0.307   0.7602  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.1657 on 46 degrees of freedom
Multiple R-squared:  0.1449,    Adjusted R-squared:  0.08914 
F-statistic: 2.598 on 3 and 46 DF,  p-value: 0.06356

[[2]]

Call:
lm(formula = Petal.Length ~ Petal.Width + Sepal.Length + Sepal.Width, 
    data = iris, subset = iris$Species == x)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.61706 -0.13086 -0.02966  0.09854  0.54311 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)   0.16506    0.40032   0.412    0.682    
Petal.Width   1.36021    0.23569   5.771 6.37e-07 ***
Sepal.Length  0.43586    0.07938   5.491 1.67e-06 ***
Sepal.Width  -0.10685    0.14625  -0.731    0.469    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.2319 on 46 degrees of freedom
Multiple R-squared:  0.7713,    Adjusted R-squared:  0.7564 
F-statistic: 51.72 on 3 and 46 DF,  p-value: 8.885e-15

[[3]]

Call:
lm(formula = Petal.Length ~ Petal.Width + Sepal.Length + Sepal.Width, 
    data = iris, subset = iris$Species == x)

Residuals:
    Min      1Q  Median      3Q     Max 
-0.7325 -0.1493  0.0516  0.1555  0.5866 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)   0.46503    0.47686   0.975    0.335    
Petal.Width   0.21565    0.17410   1.239    0.222    
Sepal.Length  0.74297    0.07129  10.422 1.07e-13 ***
Sepal.Width  -0.08225    0.15999  -0.514    0.610    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.2819 on 46 degrees of freedom
Multiple R-squared:  0.7551,    Adjusted R-squared:  0.7391 
F-statistic: 47.28 on 3 and 46 DF,  p-value: 4.257e-14

顺便说一句,你没有在代码中执行任何逐步回归。但是上面的例子可以很容易地修改来这样做。
希望这个有用。

相关问题