what is so special about purrr?

Consider this simple example:


    lm1 <- lm('Sepal.Width ~ Petal.Width', data = iris)
    lm2 <- lm('Sepal.Length ~ Petal.Length', data = iris)
    
    list(lm1, lm2) %>% stargazer::stargazer(type = 'text')
    
    ===========================================================
                                       Dependent variable:     
                                   ----------------------------
                                                NA             
                                        (1)            (2)     
    -----------------------------------------------------------
    Petal.Width                      -0.209***                 
                                      (0.044)                  
                                                               
    Petal.Length                                    0.409***   
                                                     (0.019)   
                                                               
    Constant                          3.308***      4.307***   
                                      (0.062)        (0.078)   
                                                               
    -----------------------------------------------------------
    Observations                        150            150     
    R2                                 0.134          0.760    
    Adjusted R2                        0.128          0.758    
    Residual Std. Error (df = 148)     0.407          0.407    
    F Statistic (df = 1; 148)        22.910***     468.550***  
    ===========================================================
    Note:                           *p<0.1; **p<0.05; ***p<0.01

As you can see, the dependent variable is NA... Now doing the same thing with purrr magically returns the correct dependent variables.


    list('Sepal.Width ~ Petal.Width', 'Sepal.Length ~ Petal.Length') %>% 
      map(., ~lm(formula = .x, data = iris)) %>%  stargazer::stargazer(type = 'text')
    ===========================================================
                                       Dependent variable:     
                                   ----------------------------
                                    Sepal.Width   Sepal.Length 
                                        (1)            (2)     
    -----------------------------------------------------------
    Petal.Width                      -0.209***                 
                                      (0.044)                  
                                                               
    Petal.Length                                    0.409***   
                                                     (0.019)   
                                                               
    Constant                          3.308***      4.307***   
                                      (0.062)        (0.078)   
                                                               
    -----------------------------------------------------------
    Observations                        150            150     
    R2                                 0.134          0.760    
    Adjusted R2                        0.128          0.758    
    Residual Std. Error (df = 148)     0.407          0.407    
    F Statistic (df = 1; 148)        22.910***     468.550***  
    ===========================================================
    Note:                           *p<0.1; **p<0.05; ***p<0.01

This is pretty neat but why? How is purrr sending out the correct labels here?

The problem is how you are using lm; you shouldn't quote the formulas:


library(purrr)
lm1 <- lm(Sepal.Width ~ Petal.Width, data = iris)
lm2 <- lm(Sepal.Length ~ Petal.Length, data = iris)

list(lm1, lm2) %>% stargazer::stargazer(type = 'text')
#> 
#> ===========================================================
#>                                    Dependent variable:     
#>                                ----------------------------
#>                                 Sepal.Width   Sepal.Length 
#>                                     (1)            (2)     
#> -----------------------------------------------------------
#> Petal.Width                      -0.209***                 
#>                                   (0.044)                  
#>                                                            
#> Petal.Length                                    0.409***   
#>                                                  (0.019)   
#>                                                            
#> Constant                          3.308***      4.307***   
#>                                   (0.062)        (0.078)   
#>                                                            
#> -----------------------------------------------------------
#> Observations                        150            150     
#> R2                                 0.134          0.760    
#> Adjusted R2                        0.128          0.758    
#> Residual Std. Error (df = 148)     0.407          0.407    
#> F Statistic (df = 1; 148)        22.910***     468.550***  
#> ===========================================================
#> Note:                           *p<0.1; **p<0.05; ***p<0.01

Created on 2019-02-24 by the reprex package (v0.2.1)

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.