purrr

Iterating over multiple regression models and data subsets in R

北慕城南 提交于 2020-02-03 12:16:08
问题 I am trying to learn how to automate running 3 or more regression models over subsets of a dataset using the purrr and broom packages in R. I am doing this with the nest %>% mutate(map()) %>% unnest() flow in mind. I am able to replicate examples online when there is only one regression model that is applied to several data subsets. However, I am running into problems when I have more than one regression model in my function. What I tried to do library(tidyverse) library(broom) estimate_model

Iterating over multiple regression models and data subsets in R

允我心安 提交于 2020-02-03 12:15:27
问题 I am trying to learn how to automate running 3 or more regression models over subsets of a dataset using the purrr and broom packages in R. I am doing this with the nest %>% mutate(map()) %>% unnest() flow in mind. I am able to replicate examples online when there is only one regression model that is applied to several data subsets. However, I am running into problems when I have more than one regression model in my function. What I tried to do library(tidyverse) library(broom) estimate_model

tidyverse - prefered way to turn a named vector into a data.frame/tibble

走远了吗. 提交于 2020-01-31 22:57:45
问题 Using the tidyverse a lot i often face the challenge of turning named vectors into a data.frame / tibble with the columns being the names of the vector. What is the prefered/tidyversey way of doing this? EDIT: This is related to: this and this github-issue So i want: require(tidyverse) vec <- c("a" = 1, "b" = 2) to become this: # A tibble: 1 × 2 a b <dbl> <dbl> 1 1 2 I can do this via e.g.: vec %>% enframe %>% spread(name, value) vec %>% t %>% as_tibble Usecase example: require(tidyverse)

Using purrr::pmap within mutate to create list-column

核能气质少年 提交于 2020-01-29 06:31:45
问题 I understand how to use map to iterate over arguments in a df and create a new list column. For example, params <- expand.grid(param_a = c(2, 4, 6) ,param_b = c(3, 6, 9) ,param_c = c(50, 100) ,param_d = c(1, 0) ) df.preprocessed <- dplyr::as.tbl(params) %>% dplyr::mutate(test_var = purrr::map(param_a, function(x){ rep(5, x) } )) However, how do I use the analogous syntax with pmap in the event that I want to specify more than 2 parameters? df.preprocessed <- dplyr::as.tbl(params) %>% dplyr:

Using purrr::pmap within mutate to create list-column

老子叫甜甜 提交于 2020-01-29 06:31:25
问题 I understand how to use map to iterate over arguments in a df and create a new list column. For example, params <- expand.grid(param_a = c(2, 4, 6) ,param_b = c(3, 6, 9) ,param_c = c(50, 100) ,param_d = c(1, 0) ) df.preprocessed <- dplyr::as.tbl(params) %>% dplyr::mutate(test_var = purrr::map(param_a, function(x){ rep(5, x) } )) However, how do I use the analogous syntax with pmap in the event that I want to specify more than 2 parameters? df.preprocessed <- dplyr::as.tbl(params) %>% dplyr:

Map a tbl of hyperlinks into read_html

我怕爱的太早我们不能终老 提交于 2020-01-25 06:54:59
问题 I have a tibble containing one column which stores hyperlinks in each column. Now I want to map over these links using map_dfr, passing the links one after another through read_html(.x[.x]) %>% html_node(".body-copy-lg") %>% html_text . If I do so I always end up with the error : Error in doc_parse_file(con, encoding = encoding, as_html = as_html, options = options) : Expecting a single string value: [type=character; extent=3]. Which tells me that the read_html basically says: " Hey stop

Map a tbl of hyperlinks into read_html

跟風遠走 提交于 2020-01-25 06:54:26
问题 I have a tibble containing one column which stores hyperlinks in each column. Now I want to map over these links using map_dfr, passing the links one after another through read_html(.x[.x]) %>% html_node(".body-copy-lg") %>% html_text . If I do so I always end up with the error : Error in doc_parse_file(con, encoding = encoding, as_html = as_html, options = options) : Expecting a single string value: [type=character; extent=3]. Which tells me that the read_html basically says: " Hey stop

How do I write a function to convert a character vector into a character vector of unique pairs of its elements?

被刻印的时光 ゝ 提交于 2020-01-24 19:28:08
问题 What the input would be: c("a", "b", "c") [1] "a" "b" "c" I want a function that returns: [1] "a;b" "a;c" "b;c" I need this function to work solely off its inputs. I've tried some stuff with purrr::map() and purrr::reduce() , but I haven't managed to get anything useful. 回答1: We can use combn from base R with FUN argument as paste combn(x, 2, FUN = paste, collapse = ";") #[1] "a;b" "a;c" "b;c" data x <- c("a", "b", "c") 回答2: Not the exact result but maybe it might be useful: test<-c("a", "b",

How to pass a dataframe and uneven vectors as parameters in purrr map

≡放荡痞女 提交于 2020-01-24 15:41:10
问题 I have a function with mixed data types. It takes a data frame and string variable as the input parameter. library(dplyr) myfunc <- function (dat=NULL,species=NULL,sepal_thres=NULL) { dat %>% filter(Species==species & Sepal.Length <= sepal_thres) } myfunc(dat=iris,species="virginica",sepal_thres=5) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 4.9 2.5 4.5 1.7 virginica But I want to apply it with list of vectors species_vecs <- c("virginica","setosa") sepal_thres_vecs <- c

Estimating multiple `lm` models and returning output in one table, with map()

半城伤御伤魂 提交于 2020-01-24 12:33:16
问题 I need to estimate a number of linear models on the same dataset, and put the regression results all into one table. For a reproducible example, here's a simplification using mtcars : formula_1 = "mpg ~ disp" formula_2 = "mpg ~ log(disp)" formula_3 = "mpg ~ disp + hp" Currently, my approach has been to: Create a list that contains all of the formulae. use purrr:map() to estimate all of the lm models. use stargazer:: to produce output tables. library(tidyverse) library(stargazer) formula_1 =