1

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.

Eli Groves
  • 109
  • 1
  • 6

1 Answers1

2

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"))) 
A. Suliman
  • 12,923
  • 5
  • 24
  • 37
  • Could you explain why it is `.` in some cases and `.x` in other cases? – Eli Groves Dec 11 '19 at 16:45
  • 1
    @EliGroves `do` uses `.` while `map` uses `.x` or `.`; see `?do` and `?map`. In both cases `.` and `.x` _refer to the subset of rows of .tbl for the given group_. Also, you can see this [SO](https://stackoverflow.com/questions/56532119/dplyr-piping-data-difference-between-and-x) – A. Suliman Dec 12 '19 at 03:49