Eli Groves
Eli Groves

Reputation: 109

Group-by with Fractional Response Model in R

I am trying to run a fractional response model to predict test score, using the frm package. My code is as follows:

frac.test <- sample.test %>% 
  group_by(year, state) %>% 
  do(model = frm(y = sample.test$perc.prof, 
                 x = sample.test[,c("sch.enr", "perc.wh", "perc.bl", "perc.hs", "perc.as", "perc.econ.dis", "perc.ell", "urban.flag", 
                                    "charter.flag", "high", "middle")],
                 linkfrac = "logit"))

I want the regression to be grouped by year and state, but currently the results aren't being grouped properly. I can confirm this because if I run the model for a single year and state, I get different predictions. I realize this is similar to the question: Linear Regression and group by in R however, it isn't quite the same due to the difference in syntax for frm(). I think because I am calling y = sample.test$perc.prof I am losing the grouping, but I am not sure how to fix it. I've also tried with the nest and map method:

frac.test2 <- sample.test %>% 
  nest(-year, -state) %>% 
  mutate(fit = map(data, ~ frm(y = sample.test2$perc.prof, 
                 x = sample.test2[,c("sch.enr", "perc.wh", "perc.bl", "perc.hs", "perc.as", "perc.econ.dis", "perc.ell", "urban.flag", 
                                    "charter.flag", "high", "middle")],
                 linkfrac = "logit")))

I am happy to use either method, I just want one that works properly. My data can be found here: http://www.sharecsv.com/s/bf1c215a9c306a7429b314660d31914b/frm_SO_data.csv Thank you for your time.

Upvotes: 1

Views: 243

Answers (1)

A. Suliman
A. Suliman

Reputation: 13135

Access the dataset inside mutate using .x or . not the entire dataset i.e. sample.test. use . instead of ample.test in the 1st method.

library(dplyr)
library(frm)
frac.test2 <- sample.test %>% 
  nest(-year, -state) %>% 
  mutate(fit = map(data, ~ frm(y=.x$perc.prof, x=.x[,c("sch.enr", "perc.wh", "perc.bl", "perc.hs", "perc.as", "perc.econ.dis", "perc.ell", "urban.flag", 
                                    "charter.flag", "high", "middle")],
                 linkfrac = "logit"))) 

Upvotes: 2

Related Questions