rdplyrgroup-bystochastic

Run several SFA models by a group and store the output in a list


I am running a Stochastic Frontier model (using the package frontier) by the group industry as follows:

  1. data is a panel data frame with index year and individual id and columns as below:
  2. Columns: y1, x1, x2and x3 are all numerical variables. industry is a character variable.
library(dplyr)
library(frontier)
sfa_out <- data %>%
  group_by(industry) %>%
  do(
    mod <- sfa(log(y1) ~ log(x1) + log(x2) + log(x3),
               ineffDecrease = T,
               truncNorm = F,
               timeEffect = T,
               data = .))

I want mod to store the output of the industry-group SFA estimated models. I don't think SFA-specific knowledge is required here. Thanks.


Solution

  • This did the trick:

    library(dplyr)
    library(frontier)
    library(plm)
    
    sfa_out <- data %>%
      group_by(industry) %>%
      do(
        mod = sfa(log(y1) ~ log(x1) + log(x2) + log(x3),
                   ineffDecrease = T,
                   truncNorm = F,
                   timeEffect = T,
                   data = pdata.frame(., index = c("individual id", "year"))))
    

    To then display each model you could do this:

    # to display all industry models 
    sfa_out$mod
    # to display specific industry model
    sfa$mod[[1]]
    sfa$mod[[2]]
    .
    .
    .
    sfa$mod[[n]]
    # to get the estimated efficiency measure for nth model in sfa_out$mod
    efficiencies(sfa_out$mod$[[n]])