mountainscaler
mountainscaler

Reputation: 179

Apply Simple Linear Regression to Multiple Data Frames in R

I have a dataset that I split into multiple data frames and need to apply simple linear regression to each of the split-out data frames. My code is as follows:

library(dplyr)
library(readr)
library(magrittr)
library(lubridate)
library(stats)

c_data <- read_csv("D:/projects/sloper_tool/data_2013_to_2017.csv")

C_data_out <-
c_data %>%
  group_by(SAMP_SITE_NAME, STD_CON_LONG_NAME, FILTERED_FLAG) %>%
  mutate(MED_V = median(STD_VALUE_RPTD)) %>%
  mutate(MIN_V = min(STD_VALUE_RPTD)) %>%
  mutate(MAX_V = max(STD_VALUE_RPTD)) %>%
  ungroup() %>%
  select(SAMP_SITE_NAME, STD_CON_LONG_NAME, SAMP_DATE, STD_VALUE_RPTD, STD_ANAL_UNITS_RPTD, FILTERED_FLAG, LAB_QUALIFIER, MED_V, MIN_V, MAX_V) %>%
  rename(Well = SAMP_SITE_NAME, Constit = STD_CON_LONG_NAME, Date = SAMP_DATE, Value = STD_VALUE_RPTD, Unit = STD_ANAL_UNITS_RPTD, Filtered = FILTERED_FLAG, Flag = LAB_QUALIFIER, Median = MED_V, Min = MIN_V, Max = MAX_V) %>%
  mutate(Date = mdy(Date))

dfs <- split(C_data_out, with(C_data_out, interaction(Well, Constit, Filtered)), drop = TRUE)
dfs[2]

This splits out data frames from original inputs that look like the following:

$`299-E13-14.Gross alpha.N`
# A tibble: 4 x 10
    Well     Constit       Date Value  Unit Filtered  Flag Median   Min   Max
   <chr>       <chr>     <date> <dbl> <chr>    <chr> <chr>  <dbl> <dbl> <dbl>
1 299-E13-14 Gross alpha 2014-04-11  3.40 pCi/L        N  <NA>  2.745  1.86  3.89
2 299-E13-14 Gross alpha 2015-04-08  2.09 pCi/L        N  <NA>  2.745  1.86  3.89
3 299-E13-14 Gross alpha 2016-04-25  3.89 pCi/L        N  <NA>  2.745  1.86  3.89
4 299-E13-14 Gross alpha 2017-04-06  1.86 pCi/L        N  <NA>  2.745  1.86  3.89

Next I need to apply a simple linear regression model to each of the split out data frames. I tried using various permutations of the following to no avail.

fit <-
dfs %>%
  lm(Value ~ Date)

# Get slope by:

slope <-  fit$coefficients[[2]]
slope

The output from this gives:

fit <- 
dfs %>%
  lm(Value ~ Date, data = dfs)

Error in formula.default(object, env = baseenv()) : invalid formula

slope = fit$coefficients[[2]]

Error: object 'fit' not found

slope
(Intercept)          Date 
109778.966473     -5.093003

This appears to be being applied to the entire original dataset and not to the individual split out data frames. I would like to output the slopes of the individual data frames to a file or better yet have the slopes appended as a vector to the data frames in dfs.

Any and all help would be greatly appreciated!

Upvotes: 2

Views: 2294

Answers (1)

rosscova
rosscova

Reputation: 5580

Something like this might work. I don't have your data though, so can't test.

# calculate the fit models per data frame
fits <- lapply( dfs, function(x) {
  lm( formula = Value ~ Date, data = x )
} )

# extract the slope from all models
slopes <- sapply( fits, function(x) x$coefficients )

# print one of the results to see it
slopes[1]

Upvotes: 1

Related Questions