R - Fitting a model per subject using data.table or dplyr


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?

<strong>EDIT:</strong> 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.


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"))


  • how to access the label inside datatemplate
  • How to change the mobile app ICON inside mobile quality assurance?
  • matching first two letters of the first two words
  • rails:how to create dynamic drop-down box in rails?
  • Complex conditional element in accumulate in Jess rules
  • ORA-00907 when trying to create a CHECK constraint
  • R kmeans final distance to to centroid
  • Row numbers differ (NA vs 1) when adding first row to empty data.frame
  • Splitting data and fitting distributions efficiently
  • Batch: Concatenate two arbitrary strings outside SETLOCAL EnableDelayedExpansion
  • Removing back pointers with the use of explicit instantiation declarations causes std::bad_weak_ptr
  • Sypder 2 IDE - keep the focus in the editor after sending selected commands to interpreter
  • How to create Time-to-Event variable?
  • Drools - Is there a direct correlation between number of rules and permgen space?
  • Does NotificationManagerCompat only have wear-specific features in relation to NotificationManager?
  • read known file extensions / types from the registry
  • Does NotificationManagerCompat only have wear-specific features in relation to NotificationManager?
  • Observation info was leaked
  • read known file extensions / types from the registry
  • Capturing/Saving the current state of d3.js visualization
  • Jquery ajax form array submit
  • PHP SQL database query error message
  • How to Aggregate Relational Data in Stata?
  • How to find F(x,0) when F(x,i) = F(x-1,i) xor F(x-1, i+1) xor … F(x-1,n) in less than linear time
  • Subsetting DataFrame in R by duplicate values for Year by lowest value for Rating
  • Wrong Range Rate with Pyephem
  • Displaying image retrieved from database to image control
  • replacing while loop with list comprehension
  • Adding Parent and Child Nodes in TreeView from Sql Server 2008
  • Retrieving value from sql ExecuteScalar()
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How does Linux kernel interrupt the application?
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Why do underscore prefixed variables exist?
  • java string with new operator and a literal