yakamafish
yakamafish

Reputation: 1

apply series of commands to split data frame

I'm having some difficulties figuring out how to approach this problem. I have a data frame that I am splitting into distinct sites (link5). Once split I basically want to run a linear regression model on the subsets. Here is the code I'm working with, but it's definitely not correct. Also, It would be great if I could output the model results to a new data frame such that each site would have one row with the model parameter estimates - that is just a wish and not a necessity right now. Thank you for any help!

 les_events <- split(les, les$link5)

 result <- lapply(les_events) {

   lm1 <-lm(cpe~K,data=les_events)
   coef <- coef(lm1)
   q.hat <- -coef(lm1)[2] 
   les_events$N0.hat <- coef(lm1[1]/q.hat)
}

Upvotes: 0

Views: 224

Answers (1)

mnel
mnel

Reputation: 115382

You have a number of issues.

  • You haven't passed a function (the FUN argument) to lapply
  • Your closure ( The bit inside {} is almost, but not quite the body you want for your function)

something like th following will return the coefficients from your models

 result <- lapply(les_events, function(DD){
   lm1 <-lm(cpe~K,data=DD)
   coef <- coef(lm1)

   data.frame(as.list(coef))
  })

This will return a list of data.frames containing columns for each coefficient.

lapply(les_events, lm, formula = 'cpe~K')

will return a list of linear model objects, which may be more useful.

For a more general split / apply / combine approaches use plyr or data.table

data.table

library(data.table)
DT <- data.table(les)

result <- les[, {lm1 <- lm(cpe ~ K, data = .SD)
                 as.list(lm1)}, by = link5]

plyr

library(plyr)


result <- ddply(les, .(link5), function(DD){
   lm1 <-lm(cpe~K,data=DD)
   coef <- coef(lm1)

   data.frame(as.list(coef))
  })

# or to return a list of linear model objects
dlply(les, link5, function(DD){ lm(cpe ~K, data =DD)})

Upvotes: 3

Related Questions