0

I have a set of observations for many subjects and I would like to fit a model for each subject.

I"m using the packages data.table and fitdistrplus, but could also try to use dlpyr.

Say my data are of this form:

#subject_id #observation
1           35
1           38
2           44
2           49

Here's what I've tried so far:

 subject_models <- dt[,fitdist(observation, "norm", method = "mme"), by=subject_id]

This causes an error I think because the call to fitdist returns a fitdist object which is not possible to store in a datatable/dataframe.

Is there any intuitive way to do this using data.table or dplyr?

EDIT: A dplyr answer was provided, but I would appreciate a data.table one as well, I'll try to run some benchmarks against the two.

Bar
  • 2,736
  • 3
  • 33
  • 41
  • 1
    Stick the model in a list column till you can simplify it. [Here's a video about doing it in the tidyverse.](http://edinbr.org/edinbr/2016/05/11/may-Hadley-Update2-PostingTalk.html) – alistaire Jul 06 '16 at 18:16

1 Answers1

2

This can be easily achieved with the purrr package

I assume its the same thing @alistaire suggested

library(purrr)
library(dplyr)
library(fitdistrplus)
dt %>% split(dt$subject_id) %>%  map( ~ fitdist(.$observation, "norm", method = "mme"))

Alternatively, without purrr,

dt %>% split(dt$subject_id) %>%  lapply(., function(x) fitdist(x$observation, "norm", method = "mme"))
Sumedh
  • 4,835
  • 2
  • 17
  • 32