0

At first, I use RStudio. I have a data frame (APD) and I would like to fit the w.r.t to the factor Serial_number. The fit is a lm fit. Then I would like to use this fit to do a calibration (calibrate() out of the investr-package).

Here is an example picture of my data:

Data

Here's the data: Data

Currently I use following lines to fit via Serial_number:

Coefficients<- APD %>% 
  group_by(Serial_number) %>% 
  do(tidy(fit<- lm(log(log(Amplification)) ~ Voltage_transformed, .)))

But here, I cannot apply the calibrate()-function. Calibrate function needs an object, that inherits from "lm". And tidy only works for S3/S4-objects.

Do you have an idea?

Ben
  • 1,432
  • 4
  • 20
  • 43
  • 1
    Since you haven't provided a [reproducible example](https://stackoverflow.com/a/5963610/5619526), here's a way using the `mtcars` data set: `do.call('rbind', by(mtcars, mtcars$cyl, function(d) broom::tidy(lm(mpg ~ hp, data = d))))`. In this case `cyl` would be the grouping variable – bouncyball Jun 29 '17 at 23:19
  • Thanks a lot! Wasn't aware of this example. WIll add it later and try your advice. – Ben Jun 30 '17 at 00:00
  • @bouncyball I'm not able to apply a predict function within by.. can you please have a look? – Ben Jul 12 '17 at 19:24

1 Answers1

1

In your posted code, you're trying to rbind the predicted values from each model, not the coefficients. The function for coefficients is just coefficients(object).

I would also suggest un-nesting your code, since that makes it hard to read and change later on. Here are two generalized functions (each make assumptions, so edit as needed):

lm_by_variable <- function(data_, formula_, byvar) {
  by(
    data_,
    data_[[byvar]],
    FUN      = lm,
    formula  = formula_,
    simplify = FALSE
  )
}


combine_coefficients <- function(fit_list) {
  all_coefficients <- lapply(fit_list, coefficients)
  do.call('rbind', all_coefficients)
}

lm_by_variable(...) should be pretty self-evident: group by byvar, use lm with the given formula on each subset, and don't simplify the result. Simplifying results is really only useful for interactive work. In a script, it's better to know exactly what will be returned. In this case, a list.

The next function, combine_coefficients(...) returns a matrix of the fitted coefficients. It assumes every fitted model in fit_list has the same terms. We could add logic to make it more robust, but that doesn't seem necessary in this case.

Nathan Werth
  • 5,093
  • 18
  • 25