【问题标题】:How to run the same code on multiple different datasets in R如何在 R 中的多个不同数据集上运行相同的代码
【发布时间】:2017-02-28 23:55:34
【问题描述】:

我正在尝试对六组不同的数据进行一系列固定效应线性回归。对于每个数据集,我想对数据子集多次运行回归。

我已经为一个数据集开发了一次执行此操作的代码。但我想编写通用代码,以便我可以为六组单独的数据中的每组运行它。

这是我目前使用的示例数据集:

month <- (rep(0:35, 36))
monthfact <- as.factor(month)
prodid2<- as.character(rep(112:147, 36))
log_value <- rnorm(1296)
exp_share <- abs(rnorm(1296))
regdat <- data.frame(month, monthfact, prodid2, log_value, exp_share)
#Subset the data into 24 datasets, each of which includes a 13 month window
subfun<-function(x,y,z) {  subset(x,y>=z & y<=z+12)}
dsets <- lapply(1:24, function(x) subfun(regdat, regdat$month, x-1)) 
#Writing a function for running linear regressions

lmfun<-function(data){  lm(log_value~monthfact+prodid2, data = data, 
weights = data$exp_share)}
#Apply the function to all the datasets in the list
linreg<-lapply(dsets,lmfun)
coefs<-lapply(linreg,coef)
#Choose only the coefficients for month 
coefs <- as.data.frame(lapply(coefs, function(x) {x[2:13]}))
#Add in a row of 0 values for the baseline month
baseline<-rep(0,each=24)
coefs<-rbind(baseline,coefs)

#Compute the index using the dataframe created
FEindexes<-data.frame(lapply(coefs, function(x) (exp(x))/(exp(x[1]))))
splices<-FEindexes[2,]
splices <- apply(splices, 1, cumprod)
splices <- c(1,splices[1:23])
FEindex13<-t(FEindexes[13,])
FEWS<-splices*FEindex13
FEWS<-as.data.frame(FEWS[2:24])
firstFEWS<-as.data.frame(FEindexes[,1])
colnames(firstFEWS) <- "FEWS_index"
colnames(FEWS) <- "FEWS_index"
FEWS<-rbind(firstFEWS,FEWS)
View(FEWS)

我想在 6 个不同的数据集上运行所有这些代码,并想知道是否有一种方法可以在 R 中执行此操作而无需重新运行所有代码 6 次?

非常感谢您的帮助。

【问题讨论】:

  • 我建议将 data.frames 放入一个列表中,然后使用 lapply 遍历它们。有关一些提示,请参阅 gregor 对this post 的回答。
  • 看来你 lmfun 正在做你正在寻找的东西。不是吗?但是你停在了回归水平。现在您需要通过将所需的过程包装在一个函数中来更进一步

标签: r regression apply linear-regression


【解决方案1】:

你的示例代码有点复杂,所以我将用一个更简单的例子来解释它:

如果您可以拆分您的 R 脚本,您可以使用一个包含您要执行的所有功能的脚本和第二个脚本,您可以在其中通过 source(...)调用第一个脚本> 使用不同的数据集。 非常简单的例子:将此脚本保存为“my_functions.R”在您的工作目录中(或在调用 source() 时指定文件位置):

plot(my.data)

假设您有包含所有数据集的列表(但也适用于数据框列或任何结构),请通过“source()”调用第一个脚本:

list.of.my.data <- list(a=1:10, b=11:20, c=21:30)
for (i in 1:length(list.of.my.data)){
  my.data <- list.of.my.data[[i]]
  source("my_functions.R")
  }

相反,如果您希望将所有内容保存在一个 R 脚本中,您可以编写一个 huuuge 函数,并使用每个数据集作为输入调用此函数:

# Example: set of data frames in a list
list.of.data.sets <- list(a=data.frame(x=1:10, y=1:10),
  b=data.frame(x=1:10, y=11:20),
  c=data.frame(x=1:10, y=21:30)
  )
# The meta function where you define all the things you want to do to your data sets:
my.meta.function <- function(my.data, color.parameter, size.parameter){
  plot(y~x, data=my.data, cex=size.parameter, col=color.parameter) 
  my.mean <- mean(my.data$y)
  return(my.mean)
  }
# Call the function for each data set with a for-loop:
for(i in 1:length(list.of.data.sets)){
    my.meta.function(my.data=list.of.data.sets[[i]], size.parameter=4, color.parameter=20)
    }
# Call the function for each data set with lapply (faster!):
results.of.all.data.sets <- lapply(list.of.data.sets, FUN=my.meta.function, size.parameter=4, color.parameter=20)

【讨论】:

    猜你喜欢
    • 2021-09-27
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 2020-10-03
    • 2021-10-04
    • 2020-07-14
    相关资源
    最近更新 更多