broom

Creating models and augmenting data without losing additional columns in dplyr/broom

时光毁灭记忆、已成空白 提交于 2019-12-01 16:31:02
Consider the following data / example. Each dataset contains a number of samples with one observation and one estimate: library(tidyverse) library(broom) data = read.table(text = ' dataset sample_id observation estimate A A1 4.8 4.7 A A2 4.3 4.5 A A3 3.1 2.9 A A4 2.1 2 A A5 1.1 1 B B1 4.5 4.3 B B2 3.9 4.1 B B3 2.9 3 B B4 1.8 2 B B5 1 1.2 ', header = TRUE) I want to calculate a linear model per dataset to remove any linear bias between observation and estimate, and get the fitted values next to the original ones: data %>% group_by(dataset) %>% do(lm(observation ~ estimate, data = .) %>% augment

Creating models and augmenting data without losing additional columns in dplyr/broom

故事扮演 提交于 2019-12-01 15:11:08
问题 Consider the following data / example. Each dataset contains a number of samples with one observation and one estimate: library(tidyverse) library(broom) data = read.table(text = ' dataset sample_id observation estimate A A1 4.8 4.7 A A2 4.3 4.5 A A3 3.1 2.9 A A4 2.1 2 A A5 1.1 1 B B1 4.5 4.3 B B2 3.9 4.1 B B3 2.9 3 B B4 1.8 2 B B5 1 1.2 ', header = TRUE) I want to calculate a linear model per dataset to remove any linear bias between observation and estimate, and get the fitted values next

Function for Tidy chisq.test Output for Visualizing or Filtering P-Values

雨燕双飞 提交于 2019-12-01 11:39:48
For data... library(productplots) library(ggmosaic) For code... library(tidyverse) library(broom) I'm trying to create tidy chisq.test output so that I can easily filter or visualize p-values. I'm using the "happy" dataset (which is included with either of the packages listed above) For this example, if I wanted to condition the "happy" variable on all other variables,I would isolate the categorical variables (I'm not going to create factor groupings out of age, year, etc, for this example), and then run a simple function. df<-happy%>%select(-year,-age,-wtssall) lapply(df,function(x)chisq.test

rolling regression by group in the tidyverse?

孤者浪人 提交于 2019-11-29 10:04:55
There are many questions about rolling regression in R, but here I am specifically looking for something that uses dplyr , broom and (if needed) purrr . This is what makes this question different. I want to be tidyverse consistent. Is is possible to do a proper running regression with tidy tools such as purrr:map and dplyr ? Please consider this simple example: library(dplyr) library(purrr) library(broom) library(zoo) library(lubridate) mydata = data_frame('group' = c('a','a', 'a','a','b', 'b', 'b', 'b'), 'y' = c(1,2,3,4,2,3,4,5), 'x' = c(2,4,6,8,6,9,12,15), 'date' = c(ymd('2016-06-01', '2016

Comparison between dplyr::do / purrr::map, what advantages? [closed]

霸气de小男生 提交于 2019-11-28 16:43:15
When using broom I was used to combine dplyr::group_by and dplyr::do to perform actions on grouped data thanks to @drob. For example, fitting a linear model to cars depending on their gear system: library("dplyr") library("tidyr") library("broom") # using do() mtcars %>% group_by(am) %>% do(tidy(lm(mpg ~ wt, data = .))) # Source: local data frame [4 x 6] # Groups: am [2] # am term estimate std.error statistic p.value # (dbl) (chr) (dbl) (dbl) (dbl) (dbl) # 1 0 (Intercept) 31.416055 2.9467213 10.661360 6.007748e-09 # 2 0 wt -3.785908 0.7665567 -4.938848 1.245595e-04 # 3 1 (Intercept) 46.294478

rolling regression by group in the tidyverse?

三世轮回 提交于 2019-11-28 00:19:50
问题 There are many questions about rolling regression in R, but here I am specifically looking for something that uses dplyr , broom and (if needed) purrr . This is what makes this question different. I want to be tidyverse consistent. Is is possible to do a proper running regression with tidy tools such as purrr:map and dplyr ? Please consider this simple example: library(dplyr) library(purrr) library(broom) library(zoo) library(lubridate) mydata = data_frame('group' = c('a','a', 'a','a','b', 'b

using lm in list column to predict new values using purrr

浪子不回头ぞ 提交于 2019-11-27 23:23:28
I am trying to add a column of predictions to a dataframe that has a list column that contains an lm model. I adopted some of the code from this post . I have made a toy example here: library(dplyr) library(purrr) library(tidyr) library(broom) set.seed(1234) exampleTable <- data.frame( ind = c(rep(1:5, 5)), dep = rnorm(25), groups = rep(LETTERS[1:5], each = 5) ) %>% group_by(groups) %>% nest(.key=the_data) %>% mutate(model = the_data %>% map(~lm(dep ~ ind, data = .))) %>% mutate(Pred = map2(model, the_data, predict)) exampleTable <- exampleTable %>% mutate(ind=row_number()) that gives me a

using lm in list column to predict new values using purrr

送分小仙女□ 提交于 2019-11-26 21:03:37
问题 I am trying to add a column of predictions to a dataframe that has a list column that contains an lm model. I adopted some of the code from this post. I have made a toy example here: library(dplyr) library(purrr) library(tidyr) library(broom) set.seed(1234) exampleTable <- data.frame( ind = c(rep(1:5, 5)), dep = rnorm(25), groups = rep(LETTERS[1:5], each = 5) ) %>% group_by(groups) %>% nest(.key=the_data) %>% mutate(model = the_data %>% map(~lm(dep ~ ind, data = .))) %>% mutate(Pred = map2