Below are code for the multiple testings on one variable (see):
## read data
library(foreign)
ml <- read.dta("http://www.ats.ucla.edu/stat/data/hsbdemo.dta")
## define the reference group
ml$prog2 <- relevel(ml$prog, ref = "academic")
## starting to do the test, here is one result
test <- multinom(prog2 ~ ses, data = ml)
summary(test) ## need to display this
### caculate the z value
z <- summary(test)$coefficients/summary(test)$standard.errors
z ## this output need to display
### caculate the p value
# 2-tailed z test
p <- (1 - pnorm(abs(z), 0, 1)) * 2
p ## this output need to display.
Since in the date sets have multiple variable (for example: "female" "ses" "schtyp"), i tried to write a loop to generate all the required output indicated above, using the below code :
## read the data
ml <- read.dta("http://www.ats.ucla.edu/stat/data/hsbdemo.dta")
ml$prog2 <- relevel(ml$prog, ref = "academic")
## Here define the varlist
varlist <- names(ml)[2:4]
varlist
### define the for loop to do the analysis for the three varible defined above
models <- lapply(varlist, function(x) {
multinom(substitute(prog2 ~ i, list(i = as.name(x))), data = ml)
})
models
By doing this, it can only display the output for the first required output, how to improve this to also include the other two output? or we add another function within the function?