Skip to content

Instantly share code, notes, and snippets.

@Nate-Wessel
Created January 27, 2017 00:16
Show Gist options
  • Save Nate-Wessel/903c2c468d96196df45510b80b4cf622 to your computer and use it in GitHub Desktop.
Save Nate-Wessel/903c2c468d96196df45510b80b4cf622 to your computer and use it in GitHub Desktop.
playing around with binomial regression in R
# toy code for playing with discrete choice models and logistic regression
# sample size
n = 2000
# generate random independents
x1 = scale(runif(n))
x2 = scale(runif(n))
x3 = scale(runif(n))
# 'actual' utility function that I made up
uy = -3 + -2 * x1 + 2 * x2 + 0.5 * x3
# probability derived from logistic function (inverse of logit)
py = 1/(1+exp(-uy))
# generate 0 or 1 with probabilities from above
y = ifelse(runif(n)>py,0,1)
# combine into data frame
d = data.frame(y,x1,x2,x3)
# convert to factor
d$y = factor(y)
# do the model!
r = glm(y ~ x1 + x2 + x3, data=d, family='binomial')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment