简体   繁体   English

将简单线性回归应用于 R 中的多个数据帧

[英]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.我想将单个数据帧的斜率输出到一个文件,或者更好的是将斜率作为向量附加到 dfs 中的数据帧。

Any and all help would be greatly appreciated!任何和所有的帮助将不胜感激!

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM