簡體   English   中英

使用 fct_collapse 標記值並僅對數據框列表中的一組運行回歸

[英]Labeling values using fct_collapse and running regressions for only a group within a data frame list

我正在嘗試用列表做多項事情。 對於可重現的示例,我將使用 Iris。 例如,假設我有一個包含多個 iris 數據集的列表。 就我而言,值不同。 對於此示例,使用 iris*2 不起作用,因為當您進行乘法運算時,物種會返回 NA。 假設它們是不同的。 Iris1 <- iris Iris2 <- iris Iris3 <- iris data_list <- list(Iris1, Iris2, Iris3)然后我試圖用 dplyr 運行 fct_collapse 到帶有 lapply 的數據列表,但沒有成功。 單獨來看,它就像Iris1 <- Iris1%>% group_by(Sepal.Length = fct_collapse(as.character(Sepal.Length), Group1 = as.character(5:7), Group2 = as.character(3:5)))運行到data_list 后,我​​想運行一個lm() 到data_list,基於組過濾。 因此,在 data_list 中的所有數據幀中,僅為 Group1 運行 lm(Sepal.Length~Petal.Length)。

因此,如果我很好地理解了問題,請在此處找到使用嵌套 data.frame 的建議解決方案。 這將讓您以優雅的方式為每個組和數據集創建一個 lm。

library(tidyverse)
library(magrittr)

Iris1 <- Iris2 <-  Iris3 <- 
  iris %>%
  ## trick to make sepal length an integer to be in the same shape than the user
  ## create a regional variable UF
  mutate(
    UF = sample(1:10, n(), replace = TRUE)
  )

## merge the dataset
data_merged <- 
  ## merge the dataset adding a dataset id that identify the dataset  
  bind_rows(
    Iris1 %>% add_column(data.set.id = 1), 
    Iris2 %>% add_column(data.set.id = 2), 
    Iris3 %>% add_column(data.set.id = 3)
  ) %>%
  ## create the regional aggreagation variable
  mutate(
    region.agg = 
      fct_collapse(
        factor(UF), 
        group1 = paste(1:3), 
        group2 = paste(4:7), 
        group3 = paste(8:10))
  )

## now we have a correctly formated data.frame you can apply a lm() per data.set
## and region. Here the model will be lm(Sepal.Length ~ Sepal.Width)
data_mod <-
  data_merged %>%
  nest(data = - any_of(c('data.set.id', 'region.agg'))) %>%
  mutate(
    mod  = map(data, ~ lm(Sepal.Length ~ Sepal.Width, data = .x))
  )

## you have now a single lm per data.frame and regional group combination
data_mod

## you can print them all 
data_mod$mod

## or access just one of interest (e.g. 2nd data.set group2)
data_mod %>%
  filter(data.set.id == 2, region.agg == 'group2') %>%
  pull('mod') %>%
  magrittr::extract2(1)

哪個應該給出:

> data_mod
# A tibble: 9 × 4
  data.set.id region.agg data              mod   
        <dbl> <fct>      <list>            <list>
1           1 group3     <tibble [40 × 6]> <lm>  
2           1 group1     <tibble [46 × 6]> <lm>  
3           1 group2     <tibble [64 × 6]> <lm>  
4           2 group3     <tibble [40 × 6]> <lm>  
5           2 group1     <tibble [46 × 6]> <lm>  
6           2 group2     <tibble [64 × 6]> <lm>  
7           3 group3     <tibble [40 × 6]> <lm>  
8           3 group1     <tibble [46 × 6]> <lm>  
9           3 group2     <tibble [64 × 6]> <lm>  

> ## you can print them all 
> data_mod$mod
[[1]]

Call:
lm(formula = Sepal.Length ~ Sepal.Width, data = .x)

Coefficients:
(Intercept)  Sepal.Width  
     7.2212      -0.3695  


.....

[[9]]

Call:
lm(formula = Sepal.Length ~ Sepal.Width, data = .x)

Coefficients:
(Intercept)  Sepal.Width  
     6.3680      -0.1712  



> ## or access just one of interest (e.g. 2nd data.set group2)
> data_mod %>%
+   filter(data.set.id == 2, region.agg == 'group2') %>%
+   pull('mod') %>%
+   magrittr::extract2(1)

Call:
lm(formula = Sepal.Length ~ Sepal.Width, data = .x)

Coefficients:
(Intercept)  Sepal.Width  
     6.3680      -0.1712  
 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM