【问题标题】:R loop - Pick up the next input and store the output into data frameR循环 - 拾取下一个输入并将输出存储到数据帧中
【发布时间】:2017-09-19 14:42:51
【问题描述】:

所以我是 R 的新手,基本上我希望从一个数据帧中获取数据并将输出存储到一个新数据帧中进行大量回归,但同时也创建一个循环以自动“拾取”下一个输入”并跳过重复项。

我附上了一张我的数据的照片。

这是我运行回归的代码

#inputs
Airport = "ABZ"


#choose target airport & nation GDP
df <- subset(Elasticities_Study, Airport_Code==Airport)

#log-log
df <- data.frame(df$Year, df$Region, 
             df$Airport_Code, log(df$Passengers, 10), log(df$GDP, 10))
colnames(df) <- c("Year", "Region", 
                         "Airport", "Passengers", "GDP")

#regression
fit <- lm(df$Passengers ~ df$GDP)

#store the coefficient
coefficient <-coefficients(fit)
elasticity <- coefficient["df$GDP"]

#store the p_value
p <- function (fit) {
  if (class(fit) != "lm") stop("Not an object of class 'lm' ")
  f <- summary(fit)$fstatistic
  p <- pf(f[1],f[2],f[3],lower.tail=F)
  attributes(p) <- NULL
  return(p)
}

p_value <- p(fit)

#store the r_squared
r_squared <- summary(fit)$r.squared

#save regression output into data frame
Regression_Output <- data.frame(df[1,2], df[1,3], 
                            elasticity, p_value, r_squared)
colnames(Regression_Output) <- c("Region", "Airport", "Elasticity", "P-
Value", "R_Squared")

有人可以帮忙吗!谢谢!

【问题讨论】:

    标签: r loops dataframe


    【解决方案1】:

    抱歉,我无法对上述答案发表评论(我的答案更像是评论,但我还没有真正的权限)。

    您可以使用执行的循环 Ben_its 的变体,但不要对 i 使用数值,而是让循环按机场代码进行迭代。它的代码乱七八糟,但我的大脑是如何工作的

    首先创建一个空白 data.frame 来存储您的新结果解决方案。 然后创建一个包含所有机场代码的向量。 将循环设置为遍历您提供的机场代码向量。

    在循环中,它做的第一件事是根据第一个机场代码对数据进行子集化。因此,在您的列表中,它只会占用您的代码向量中带有 Airport_Code == item #1 的机场。然后它将运行您的回归,您将告诉它从摘要中获取哪些值等。一旦它具有您想要的值,它将其存储到向量解决方案 new_data 中,然后将其重新绑定到空白 data.frame我们为解决方案而创建。最后一行是通过获取结果 data.frame 的前一个对象并根据机场代码的子集数据添加刚刚获得的解决方案的新行来简单地创建新的结果 data.frame 的地方。然后,它会遍历代码向量中的下一个机场代码。

    your_data = data.frame(source data)
    ## Create empty data.frame to store solutions. We will use the loop and rbind to constantly add a row of solutions with each iteration of the loop
    results = data.frame()
    codes = c(all of your airport codes)
    for (i in codes){
        ## here you use the variable of the loop to subset your whole dataset based on that airport code
        byairportcode = subset(your_data,your_data$Airport_Code == i)
        ## All operations listed in your post, creating a vector of your desired information/solutions for each line
        new_data = something(parameters)
        ###combine this new data with your list of solutions by airport code 
        results = rbind(results,new_data)
    } 
    

    您不需要消除任何重复项,因为您已经按机场代码对数据进行了子集化,因此结果框中的每个条目都是每个单独的机场代码的回归结果。

    【讨论】:

      【解决方案2】:

      考虑使用by 按每个机场代码 对大型数据帧进行切片,并将每个数据帧作为子集数据帧对象传递给调用函数,例如回归处理。

      最终输出将是与不同机场代码长度相等的数据帧列表,以及由相应机场代码命名的列表中的每个项目。

      分配函数

      # store the p_value
      p <- function (fit) {
         if (class(fit) != "lm") stop("Not an object of class 'lm' ")
         f <- summary(fit)$fstatistic
         p <- pf(f[1],f[2],f[3],lower.tail=F)
         attributes(p) <- NULL
      
         return(p)
      }
      
      # run regressions
      regression_process <- function(df) {
      
         #log-log
         df <- data.frame(Year=df$Year, Region=df$Region, Airport=df$Airport_Code,
                          Passengers=log(df$Passengers, 10), GDP=log(df$GDP, 10))
         #regression
         fit <- lm(df$Passengers ~ df$GDP)
      
         #store the coefficient
         coefficient <-coefficients(fit)
         elasticity <- coefficient["df$GDP"]
      
         p_value <- p(fit)
      
         #store the r_squared
         r_squared <- summary(fit)$r.squared
      
         #save regression output into data frame
         data.frame(Region=df[1,2], Airport=df[1,3], Elasticity=elasticity, 
                    P_Value=p_value, R_Squared=r_squared)
      }
      

      Run by() (为每个机场代码创建一个命名数据框列表)

      regression_list <- by(Elasticities_Study, Elasticities_Study$Airport_Code, 
                            FUN=regression_process)
      
      regression_list$ABE   # FIRST REGRESSION DATAFRAME ELEMENT
      regression_list$ABI   # SECOND REGRESSION DATAFRAME ELEMENT
      regression_list$ABJ   # THIRD REGRESSION DATAFRAME ELEMENT
      ...
      

      【讨论】:

        猜你喜欢
        • 2021-09-29
        • 1970-01-01
        • 2016-04-29
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-18
        • 1970-01-01
        相关资源
        最近更新 更多