lrnv
lrnv

Reputation: 1106

MagrittR T-pipe for the summary.glm function. Simple issue

I'm actually running on a problem with the T pipe. I'm trying to do 3 things in the same chain:

  1. Fit my GLM
  2. Save it in a variable
  3. Print it's summary

So I'm trying the following syntax:

my_variable <- data %>%
    glm(Responce ~ Variables, family) %T>%
    summary

Which does not work as expected. The glm get's fitted, but the summary wont show itself. So I'm force to break it into 2 chains:

my_variable <- data %>%
    glm(Responce ~ Variables, family)

my_variable %>% summary

So I'm thinking: Either I didn't get the functionality of the T-pipe, either it's not properly coded and mess around with the summary function.

Because if I try:

my_variable <- data %>%
    glm(Responce ~ Variables, family) %T>%
    plot

it works well.

Some ideas?

Upvotes: 3

Views: 609

Answers (2)

moodymudskipper
moodymudskipper

Reputation: 47310

When you just type summary(something) in the console, print is called implicitly. It's not the case in your pipe call, so you need to explicitly call print.

Because the unbranching of %T>% works for one operation only you'll have to compose print and summary :

my_variable <- data %>%
    glm(Responce ~ Variables, family) %T>%
    {print(summary(.)}

You need curly braces and the dot else the glm output would be passed as the first argument to print.

Upvotes: 6

Thomas K
Thomas K

Reputation: 3311

I don't see, why you would need %T>% here. If you want to force printing, just use regular pipes and add print() to your pipe. Keep in mind however, that with this approach you store the summary in my_variable, not the model itself.

library(magrittr)

my_variable <- my_data %>%
  glm(counts ~ outcome + treatment, family = poisson(), data = .) %>%
  summary() %>% 
  print()
#> 
#> Call:
#> glm(formula = counts ~ outcome + treatment, family = poisson(), 
#>     data = .)
#> 
#> Deviance Residuals: 
#>        1         2         3         4         5         6         7  
#> -0.67125   0.96272  -0.16965  -0.21999  -0.95552   1.04939   0.84715  
#>        8         9  
#> -0.09167  -0.96656  
#> 
#> Coefficients:
#>               Estimate Std. Error z value Pr(>|z|)    
#> (Intercept)  3.045e+00  1.709e-01  17.815   <2e-16 ***
#> outcome2    -4.543e-01  2.022e-01  -2.247   0.0246 *  
#> outcome3    -2.930e-01  1.927e-01  -1.520   0.1285    
#> treatment2   1.338e-15  2.000e-01   0.000   1.0000    
#> treatment3   1.421e-15  2.000e-01   0.000   1.0000    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> (Dispersion parameter for poisson family taken to be 1)
#> 
#>     Null deviance: 10.5814  on 8  degrees of freedom
#> Residual deviance:  5.1291  on 4  degrees of freedom
#> AIC: 56.761
#> 
#> Number of Fisher Scoring iterations: 4

Data

counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)
my_data <- data.frame(treatment, outcome, counts)

Upvotes: -1

Related Questions