0

I'd like to append lines from the output of the formula summary.

This is my formula summary--

      library(fpp)

      analysisOfVariance <-
        aov(
          score ~ single,
          credit  
        )
        
        summ <- summary(analysisOfVariance)

        # assumes:
        # independent and randomly selected observations
        # normal distributions for each factor level
        # common variance

I'd like to add the commented out text to the summary, so that the output looks like this

Call:
   aov(formula = as.formula(formula), data = dataset[c(facs$vars[x], 
    nums$vars[y])])

Terms:
                  single Residuals
Sum of Squares    834.84  95658.64
Deg. of Freedom        1       498

Residual standard error: 13.8595
Estimated effects may be unbalanced

        # assumes:
        # independent and randomly selected observations
        # normal distributions for each factor level
        # common variance
user106014
  • 41
  • 1
  • 6

1 Answers1

1

You can redefine the print.summary.aov function to include calls to cat that print your message at the end. To see the original function, just type this in your console:

stats:::print.summary.aov

I found this function because I know that the functions that print results to the console are typically called print.MODELCLASS. In your example, calling this:

class(summary(analysisOfVariance))
[1] "summary.aov" "listof"

So I knew I was looking for a function called print.summary.aov. So I just typed that in the console and found it.

Then modify it as follows:

print.summary.aov <- function (x, digits = max(3L, getOption("digits") - 3L), symbolic.cor = FALSE,
    signif.stars = getOption("show.signif.stars"), ...)
{
    if (length(x) == 1L)
        print(x[[1L]], digits = digits, symbolic.cor = symbolic.cor,
            signif.stars = signif.stars)
    else NextMethod()
    if (nzchar(mess <- naprint(attr(x, "na.action"))))
        cat(mess, "\n", sep = "")
    cat("# assumes:
         # independent and randomly selected observations
         # normal distributions for each factor level
         # common variance\n")
    invisible(x)
}

Finally:

library(fpp)
analysisOfVariance <- aov(score ~ single, credit)
summary(analysisOfVariance)

             Df Sum Sq Mean Sq F value Pr(>F)
single        1    835   834.8   4.346 0.0376 *
Residuals   498  95659   192.1
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# assumes:
# independent and randomly selected observations
# normal distributions for each factor level
# common variance
Vincent
  • 15,809
  • 7
  • 37
  • 39
  • Can you write about the way you found the print function? I'd like to do the same thing for the `TukeyHSD` function – user106014 Oct 25 '20 at 12:38
  • I tried the same thing and I got `function (x, which, ordered = FALSE, conf.level = 0.95, ...) UseMethod("TukeyHSD") ` – user106014 Oct 25 '20 at 12:39
  • I added a short comment to my post. Let me know if that doesn't work. – Vincent Oct 25 '20 at 12:41
  • I'm still somewhat confused. I'm trying to find the equivalent for `t.test` – user106014 Oct 25 '20 at 12:58
  • What is the `class` of the object you want to summarize? Call `class(model)` on it (where model is your actual fitted model). – Vincent Oct 25 '20 at 13:00
  • It's this class `[1] "htest"` and the model is t.test() – user106014 Oct 25 '20 at 13:02
  • It looks like this is an object from the `EnvStats` package. So load that package with `library(EnvStats)`, then type `print.htest` in your console to see the content of the function. This is the function that is used to print the results to your console and that you will want to modify. – Vincent Oct 25 '20 at 13:03
  • Are you sure? I did that, but EnvStats wasn't in my environment even though the t test worked and it gave me the error `Error in space(17) : could not find function "space"` – user106014 Oct 25 '20 at 13:07
  • I am 100% sure that this is the correct way to modify a printing function, in general, as demonstrated in my answer to the question. However, I am *not* sure that your model object is of a class produced by `EnvStats` package, because you have not supplied a [Minimal Reproducible Example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It could also be that `space` is not exported by the package, in which case you'll have to modify the function further by calling `EnvStats:::space` (note the three `:::`), which *does* in fact appear to exist. – Vincent Oct 25 '20 at 13:11
  • Thanks! I added a new quesiton https://stackoverflow.com/questions/64524093/how-do-i-append-lines-to-the-output-from-a-formula-summary-for-a-t-test – user106014 Oct 25 '20 at 13:14
  • Can you please updat eyour answer to be more like this one? https://stackoverflow.com/questions/64524093/how-do-i-append-lines-to-the-output-from-a-formula-summary-for-a-t-test – user106014 Oct 25 '20 at 13:33