Confusion matrix with wrong dimensions

Links, such as the book are welcome when helpful to illuminate a problem. If application of sound judgment does not screen out problematic links, the community will. Examples might include links to pirated copies, verbatim unpublished instructor assignments and links full of extraneous stuff. None of those problems appear here.

While links may be helpful, a minimal reproducible example is essential to all but questions of a general nature. See the FAQ: How to do a minimal reproducible example reprex for beginners.

The following reprex uses the link code to illustrate part of the problem— why output2 does not look like output1, by clearly showing how output1 is constructed. What is unclear is the derivation of output2.

suppressPackageStartupMessages({
  library(ISLR)
})

# Section 4.6.2 Gareth James et. al. An Introduction to Statistical Learning 1st ed.

attach(Smarket)
str(Smarket)
#> 'data.frame':    1250 obs. of  9 variables:
#>  $ Year     : num  2001 2001 2001 2001 2001 ...
#>  $ Lag1     : num  0.381 0.959 1.032 -0.623 0.614 ...
#>  $ Lag2     : num  -0.192 0.381 0.959 1.032 -0.623 ...
#>  $ Lag3     : num  -2.624 -0.192 0.381 0.959 1.032 ...
#>  $ Lag4     : num  -1.055 -2.624 -0.192 0.381 0.959 ...
#>  $ Lag5     : num  5.01 -1.055 -2.624 -0.192 0.381 ...
#>  $ Volume   : num  1.19 1.3 1.41 1.28 1.21 ...
#>  $ Today    : num  0.959 1.032 -0.623 0.614 0.213 ...
#>  $ Direction: Factor w/ 2 levels "Down","Up": 2 2 1 2 2 2 1 2 2 2 ...
glm.fits <- glm(formula = Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume, data = Smarket, family = binomial)
glm.probs <- predict(glm.fits, type = "response")
glm.pred = rep("Down", 1250)
glm.pred[glm.probs >.5] = "Up"
table(glm.pred, Direction)
#>         Direction
#> glm.pred Down  Up
#>     Down  145 141
#>     Up    457 507
detach(Smarket)

A further advantage to a reprex is that it relieves the burden of entering the code from the link and avoids frustrations such as the ambiguity between ~ and ∼ .