Created
December 1, 2017 23:05
-
-
Save carlislerainey/7cc726c27982d674082da94d86f04306 to your computer and use it in GitHub Desktop.
code to understand product terms and probit models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# code to understand product terms and probit models | |
# clear workspace | |
rm(list = ls()) | |
# load packages | |
library(tidyverse) | |
library(plotly) | |
# parameters | |
n_pts <- 100 # number of values of x (and z) at which we calcalte Pr(y = 1) | |
b_cons <- -3 # Pr(y) = Phi(b_cons + b_x*x + b_z*z + b_xz*x*z) | |
b_x <- 0 | |
b_z <- -3 | |
b_xz <- 6 | |
# create data for surface | |
x_pred <- z_pred <- seq(0, 1, length.out = n_pts) # values at which to calculate Pr(y) | |
df <- data.frame(x = x_pred, z = z_pred) %>% # put values in data frame | |
expand(x, z) %>% # expand data frame to all combinations | |
mutate(prob = pnorm(b_cons + b_x*x + b_z*z + b_xz*x*z)) # calculate Pr(y) for all combinations | |
# put Pr(y) or df$prob in a numeric matrix | |
X <- matrix(df$prob, nrow = n_pts, ncol = n_pts, byrow = FALSE) | |
# plot surface | |
plot_ly() %>% | |
add_surface(x = x_pred, y = z_pred, z = X) %>% | |
layout(scene = list(xaxis = list(title = "x"), | |
yaxis = list(title = "z"), | |
zaxis = list(title = "Probability"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment