Bruce
Code:
* Date: 20-Nov-2021
* File: qreg_nestreg_problem.do
* Problem: When using -nestreg- with -qreg-, factor
* variables with more then 2 levels cause an error.
* Example generated using Stata/IC 16.1 for Windows
clear
sysuse auto
// Use -regress- with -nestreg-
nestreg: regress mpg (c.weight) (i.rep78)
// Replace -regress- with -qreg-
capture noisily nestreg: qreg mpg (c.weight) (i.rep78), nolog
display "Error code: " _rc
// Let's try again using computed indicator variables
tabulate rep78, gen(rep)
nestreg: qreg mpg (c.weight) (rep1-rep4), nolog
// That works
// Once more with i. prefix on the indicators
nestreg: qreg mpg (c.weight) (i.rep1 i.rep2 i.rep3 i.rep4), nolog
// That also works
// So factor variables with 2 levels work, but not with 5 levels?
// How many levels cause problems?
forvalues i = 3/5 {
capture noisily nestreg: qreg mpg (c.weight) (i.rep78) if rep78 < `i', nolog
display "Levels = " `i'-1 " Error code: " _rc
display "---------------------------------------------"
}
// Looks like it falls apart with 3 or more levels to a factor variable
// Generate another factor variable with 3 levels to confirm that it is
// not something peculiar about variable rep78.
generate byte newvar = mod(_n,3)
fre newvar
capture noisily nestreg: qreg mpg (c.weight) (i.newvar), nolog
display "Error code: " _rc
// The previous line generates an r(111) error--variable b not found
// If I use only 2 levels of newvar, everything works!
nestreg: qreg mpg (c.weight) (i.newvar) if newvar < 2, nolog
nestreg: qreg mpg (c.weight) (i.newvar) if newvar > 0, nolog
nestreg: qreg mpg (c.weight) (i.newvar) if newvar != 1, nolog
0 Response to Factor variables with 3 or more levels cause r(111) error when using -nestreg- and -qreg-
Post a Comment