Created
September 12, 2022 19:33
-
-
Save JakeJing/7c721fd6ac10d50a334906cfa659e3f4 to your computer and use it in GitHub Desktop.
simulate data from multinomial logistic regression
This file contains hidden or 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
N <- 1000 | |
J <- 3 # number of predictors (intercept, slp1, slp2) | |
X <- cbind(1, rnorm(N, 0, 1), rnorm(N, 0, 1)) # N * J | |
K <- 3 # num of classes | |
beta <- rbind(c(1, -0.5, 0), | |
c(-2, 4, 0), | |
c(3, -1.5, 0)) | |
# J * K (features * classes) with the last class as the reference | |
# [cls1_intercept, cls2_intercept, cls3_intercept] | |
# [cls1_x1, cls2_x1, cls3_x1 ] | |
# [cls1_x2, cls2_x2, cls3_x2 ] | |
z <- X %*% beta | |
# rowwise softmax transformation | |
probs <- t(apply(z, 1, function(x) exp(x) / sum(exp(x)))) | |
y <- apply(probs, 1, function(x) rcat(n = 1, prob = x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment