I'm attempting to plot a simple logistic regression in R for the Smarket data set (in the library "MASS"). I've successfully completed the glm.fit process to calculate the deviance residuals and coefficients, but I'd really like to visualize the logistic regression, but cannot find a simple way to do this.

For more context, I'm using "An Introduction to Statistical Learning". This is an extension of the exercise from page 156-160 (btw, this is not a school requirement, just me trying to figure it out). I imagine this is a relatively easy problem, but I'm new to R and can't seem to get it.

Thanks.

The code I've used is provided below:

```glm.fit=glm(Direction~Lag1+Lag2+Lag3+Lag4+Lag5+Volume , data = Smarket, family=binomial)
summary(glm.fit)
coef(glm.fit)
```

is there a way to create a visualization of the logistic regression? I have not included the code to include the library, if this is needed, let me know, and I will add it.

Thanks!

Apr 11, 2022 357 views

## 1 answer to this question.

The first, third, and fourth methods of visualizing the results of a logistic regression are all borrowed from the code for Chapter 5 of Gelman and Hill's Data Analysis Using Regression & Multilevel/Hierarchical Models. Some functions from the arm package (which comes with the book) are used in the code below

`library(arm)`

Plot the probability of Direction="Up" over a variety of Lag 1 values and for three different Volume values (other Lag values are set to 0, but you can change them if you want)

```# Function to jitter class category values
jitter.binary <- function(a, jitt=.05){
ifelse (a-1==0, runif(length(a), 0, jitt), runif(length(a), 1-jitt, 1))
}

# Sequence of Lag1 values for plotting
x = seq(-5,5.7,length.out=100)

# Plot jittered Direction vs. Lag 1. This shows the actual distribution of the data.
with(Smarket, plot(Lag1, jitter.binary(as.numeric(Direction)), pch=16,cex=0.7,
ylab="Pr(Up)", xlab="Lag 1"))

# Add model prediction curves. These show the probability of Direction="Up" vs. Lag 1
# for three different fixed values of Volume.
curve(expr=invlogit(cbind(1, x,0,0,0,0,1.48) %*% coef(glm.fit)),
curve(expr=invlogit(cbind(1, x, 0,0,0,0, 0.36) %*% coef(glm.fit)),
from=-5, to=5.7, lwd=.5, add=TRUE, col="red", lty=2)
curve(expr=invlogit(cbind(1, x, 0,0,0,0, 3.15) %*% coef(glm.fit)),
from=-5, to=5.7, lwd=.5, add=TRUE, col="blue", lty=2)```

Plot Lag 1 vs. Volume, color the spots according to the Direction value, and include the decision boundary. Because the decision boundary for your real regression is a five-dimensional hyperplane, I've created a new regression with just the two predictors for this. (You may still graph the decision boundary in two dimensions for models with numerous predictors by taking 2D slices through the multidimensional predictor space.)

```# New regression model
fit2 = glm(Direction ~ Lag1 + Volume , data = Smarket, family=binomial)

# Probability of Direction="Up" for this model
Smarket\$Pred2 = predict(fit2, type="response")

# Set Prediction to "Up" for probability > 0.5; "Down" otherwise.
Smarket\$PredCat2 = cut(Smarket\$Pred2, c(0,0.5,1), include.lowest=TRUE, labels=c("Down","Up"))

# Graph Lag1 vs. Volume with coloring and point-style based on value
# of Direction
with(Smarket, plot(Lag1, Volume, pch=ifelse(Direction=="Down", 3, 1),
col=ifelse(Direction=="Down", "red", "blue"), cex=0.6))

curve(expr= -(cbind(1, x) %*% coef(glm.fit2)[1:2])/coef(glm.fit2)[3],
```

Hope this helps.

• 6,000 points

## difference between a cost function and the gradient descent equation in logistic regression?

Cost function is a way to evaluate ...READ MORE

## Logistic Regression with continuous data using sklearn in python

Despite the fact that it produces a ...READ MORE

## Plot logistic regression curve in R

The Code looks something like this: fit = ...READ MORE

## How to add regression line equation and R2 on graph?

Below is one solution: # GET EQUATION AND ...READ MORE

## How to export regression equations for grouped data?

First, you'll need a linear model with ...READ MORE

## R: Force regression coefficients to add up to 1

b1 + b2 = 1 Let us fit ...READ MORE

## Extract regression coefficient values

A quick rundown. These values are stored ...READ MORE