1

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?

1 Answer 1

1

You can return multiple items from your function with a list. But it looks like there is something odd about how multinom (which I assume is from the nnet package) tries to evaluate its parameter when you call summary() on a fitted model. I also changed your lappy to Map in order to return a named list.

varlist
[1] "female" "ses"    "schtyp"

library(nnet)
models <- Map(function(x) {
  f <- substitute(prog2 ~ i, list(i = as.name(x)))
  mm <- eval(bquote(multinom(.(f), data = ml)))
  ms <- summary(mm) 
  z <- ms$coefficients/ms$standard.errors
  p <- (1 - pnorm(abs(z), 0, 1)) * 2
  list(ms , z, p)
}, varlist)

models

this returns a nested list such that

model$female[[1]] #summary
model$female[[2]] #z-values
model$female[[3]] #p-values

The same goes for the rest of the coefficients.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.