我试图预测哪些变量会影响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的新手,所以会喜欢简单。谢谢。
1条答案
按热度按时间fdx2calv1#
问问题时遵循这些指导方针是非常重要的,尽管如此,我还是用
iris
数据集做了一个简单的例子。为了对数据集的不同部分多次运行相同的回归,可以使用
lapply()
函数,该函数对向量或列表(在本例中为物种名称)应用函数,您只需将其传递给lm()
函数中的subset
参数:运行
species
显示此变量的水平:运行
colnames(iris)
会告诉我们要使用哪些变量:之后可以运行
lapply
函数,如下所示:结果是:
顺便说一句,你没有在代码中执行任何逐步回归。但是上面的例子可以很容易地修改来这样做。
希望这个有用。