3

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
lrnv
  • 1,038
  • 8
  • 19

2 Answers2

6

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.

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
  • Thanks very much, perfect answer. DO you have some more references about thoose kind of things ? The FM doas not say that much about %T>%. – lrnv Dec 01 '17 at 15:05
  • The manual says `The tee operator works like %>%, except the return value is lhs itself, and not the result of rhs function/expression. `, which I think is pretty much all there is to know about it. you can print, plot or save (or any side effect), but it won't return anything, so if you pipe it into another expression, wether with `%>%` or `%T>%`, it will act as if the 1st `%T>%` was not there, apart from side effects. search for [magrittr] in the search box above and you'll find many cool questions/answer about these operators. – moodymudskipper Dec 01 '17 at 15:17
  • 1
    See also this answer: https://stackoverflow.com/questions/48167043/print-data-frame-dimensions-at-each-step-of-filtering/48264845#48264845 . you could use the function `pprint` and just end the pipe with `%>% pprint(summary)` . The `%T>%` pipe is not needed (though you could use it too)so no need to attach `magrittr` – moodymudskipper Jul 04 '18 at 09:23
-1

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)
Thomas K
  • 3,242
  • 15
  • 29