* SINGLE FACTOR REPEATED MEASURES ANOVA
* Joseph Hilbe,  STB : 9-25-91

program def ranova
version 3.0
if "%_*"=="" {
     di in gr "At least two existing variables must be entered."
     di in gr "Variables must be numeric"
     di in gr "Example: "
     di in wh "         ranova var1 <var2> <var...>"
     exit 198
    }
mac def _varlist "req ex min(2)"
mac def _in "opt"
mac def _if "opt"
parse "%_*"
parse "%_varlist",parse(" ")
mac def _sfn "S_FN"
qui save _U_S_E_R, replace
capture {
   if "%_if`%_in"~="" {
      keep %_if %_in
   }

qui {
  count
  mac def _origobs=_result(1)
  dropmiss %_varlist
  count
  mac def _obs=_result(1)
  gen _i=0
  gen _ssq=0
  mac def _vsum=0
  mac def _vsumsq=0
  mac def _vsum2=0
  di _n(2) in gr _col(20) "Mean" _col(42) "Standard Deviation"

  while "%_1"~="" {
   mac def _i=%_i+1
   sum %_1
   gen _var%_i = sum(%_1)
      replace _var%_i=_var%_i[_N]
   gen _varsq%_i = sum(%_1^2)
      replace _varsq%_i=_varsq%_i[_N]
   mac def _vsum=%_vsum+_var%_i
   mac def _vsumsq=%_vsumsq+_var%_i^2
   mac def _vsum2=%_vsum2+_varsq%_i
   replace _ssq=_ssq+cond(%_1==.,0,%_1)
   #delimit ;
   di in gr _col(5) "%_1" in ye _col(17) %9.4f _result(3)
                                _col(46) %9.4f sqrt(_result(4));
   #delimit cr
   mac shift
}
gen _ssq2=sum(_ssq^2)
mac def _ssq2=_ssq2[_N]
mac def _crt=%_vsum^2/(%_obs*%_i)
mac def _sst=%_vsum2-%_crt
mac def _sstr=(%_vsumsq/%_obs)-%_crt
mac def _sss=(%_ssq2/%_i)-%_crt
mac def _sse=%_sst-%_sss-%_sstr
mac def _dfsst=(%_obs*%_i)-1
mac def _dfsss=%_obs-1
mac def _dfsstr=%_i-1
mac def _dfsse=%_dfsst-%_dfsss-%_dfsstr
mac def _mstr=%_sstr/%_dfsstr
mac def _mse=%_sse/%_dfsse
mac def _F = %_mstr/%_mse
mac def _Fprob = fprob(%_dfsstr, %_dfsse, %_F)
}

noi di _n(2) _col(18) in gr "Single Factor Repeated Measures ANOVA"
noi di _n(1) _col(8)  in gr "Number of obs in model = " in ye %_obs /*
      */   _col(44) in gr "Number of vars = " in ye %_i
noi di _col(8) in gr "Number of obs dropped  = " in ye %_origobs-%_obs
noi di _n(1) _col(8)  in gr "Source |     SS          df       MS        F      Prob > F"
noi di in gr _dup(70) "_"
noi di _col(6) in gr "Subjects |" in ye %9.4f _col(17) %_sss _col(32) %_dfsss /*
*/ _col(40) "  -" _col(50) "  -" _col(60) "  -"
noi di _col(4) in gr "Treatments |" in ye %9.4f _col(17) %_sstr _col(32) %_dfsstr /*
*/ %9.4f _col(38) %_mstr %6.2f _col(50) %_F  %5.4f _col(60) %_Fprob
noi di _col(9) in gr "Error |" in ye %9.4f _col(17) %_sse _col(32) %_dfsse /*
*/ %9.4f _col(38) %_mse _col(50) "  -" _col(60) "  -"
noi di in gr _dup(70) "_"
noi di _col(9) in gr "Total |" in ye %9.4f _col(17) %_sst _col(32) %_dfsst


qui {
 cap drop _va*
 cap drop _i
 cap drop _obs
 cap drop _vsu*
 cap drop _crt
 cap drop _ss*
 cap drop _df*
 cap drop _ms*
 cap drop _F*
}
mac def _rc = _rc
qui use _U_S_E_R, clear
qui erase _U_S_E_R.dta
mac def S_FN "%_sfn"
if %_rc==0 {exit}
exit %_rc

end


capture program drop dropmiss
program define dropmiss
  version 3.0
  mac def _varlist "req ex min(1)"
  parse "%_*"
  parse "%_varlist",parse(" ")
  while "%_1"~=""  {
    drop if %_1==.
    mac shift
  }
  end