Stata: compare coefficients of factor variables using foreach (or forvalues)

StackOverflow https://stackoverflow.com/questions/23299956

  •  09-07-2023
  •  | 
  •  

문제

I am using an ordinal independent variable in an OLS regression as a categorical variable using the factor variable technique in Stata (i.e, i.ordinal). The variable can take on values of the integers from 0 to 9, with 0 being the base category. I am interested in testing if the coefficient of each variable is greater (or less) than that which succeeds it (i.e. _b[1.ordinal] >= _b[2.ordinal], _b[2.ordinal] >= _b[3.ordinal], etc.). I've started with the following pseudocode based on FAQ: One-sided t-tests for coefficients:

foreach i in 1 2 3 5 6 7 8 {
 test _b[`i'.ordinal] - _b[`i+'.ordinal] = 0
 gen sign_`i'`i+' = sign(_b[`i'.ordinal] - _b[`i+'.ordinal])
 display "Ho: i <= i+ p-value = " ttail(r(df_r), sign_`i'`i+'*sqrt(r(F)))
 display "Ho: i >= i+ p-value = " 1-ttail(r(df_r), sign_`i'`i+'*sqrt(r(F)))
}

where I want the ```i+' to mean the next value of i in the sequence (so if i is 3 then ``i+' is 5). Is this even possible to do? Of course, if you have any cleaner suggestions to test the coefficients in this manner, please advise.

Note: The model only uses a sub-sample of my dataset for which there are no observations for 4.ordinal, which is why I use foreach instead of forvalues. If you have suggestions for developing a general code that can be used regardless of missing variables, please advise.

도움이 되었습니까?

해결책

There are various ways to do this. Note that there is little obvious point to creating a new variable just to hold one constant. Code not tested.

forval i = 1/8 {
    local j = `i' + 1
    capture test _b[`i'.ordinal] - _b[`j'.ordinal] = 0
    if _rc == 0 { 
        local sign = sign(_b[`i'.ordinal] - _b[`j'.ordinal])
        display "Ho: `i' <= `j' p-value = " ttail(r(df_r), `sign' * sqrt(r(F)))
        display "Ho: `i' >= `j' p-value = " 1-ttail(r(df_r), `sign' * sqrt(r(F)))
    }
}

The capture should eat errors.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top