Dear Statalist community
I have multiple loops in my code to calculate the residuals from a regression for a certain group. To give you context: I want to calculate discretionary firm accruals (the residuals) by estimating nondiscretionary accruals (predicted values). It is on the firm-year-quarter level.
For each subgroup (denoted by sic_2_n) - subgroup (denoted by year_quarter) combination, I estimate this accrual regression (see code below). However, I constantly get the error message "r(2000) no observations".
There are lots of missing values in my variables. I do not want to clean the data first (i.e., delete all observations with missing values in any of the variables) because the regression should be based on the whole universe. My understanding is that missing observations should not matter because they do not enter the regression (I get the same error message when I delete all the observations with missing values).
If there are too few observation for this subgroup-subgroup-combination (sic_2_n & year_quarter), it should just proceed with the next one. I tried to build this additional loop with
but it seems to fail.
Additionally, I only want to predict the residual if the regression is based on at least 10 observations (hence, this condition after predict with
Do you have any suggestions what this error message refers you? Probably with my data, but I do not get it - even after applying it on a cleaned subset. A excerpt of my data is found at the bottom.
As always, help is highly appreciated.
Best regards
Pascal
I have multiple loops in my code to calculate the residuals from a regression for a certain group. To give you context: I want to calculate discretionary firm accruals (the residuals) by estimating nondiscretionary accruals (predicted values). It is on the firm-year-quarter level.
For each subgroup (denoted by sic_2_n) - subgroup (denoted by year_quarter) combination, I estimate this accrual regression (see code below). However, I constantly get the error message "r(2000) no observations".
There are lots of missing values in my variables. I do not want to clean the data first (i.e., delete all observations with missing values in any of the variables) because the regression should be based on the whole universe. My understanding is that missing observations should not matter because they do not enter the regression (I get the same error message when I delete all the observations with missing values).
If there are too few observation for this subgroup-subgroup-combination (sic_2_n & year_quarter), it should just proceed with the next one. I tried to build this additional loop with
Code:
if c(rc)==0
Additionally, I only want to predict the residual if the regression is based on at least 10 observations (hence, this condition after predict with
Code:
if e(N)>=10
As always, help is highly appreciated.
Best regards
Pascal
Code:
levelsof sic_2_n, local(sic_reg) gen absmjda1=. foreach i of local sic_reg { levelsof year_quarter, local(yearquarter_reg) foreach p of local yearquarter_reg { regress acc_1 inverseassets adj_deltasales ppe i.quint_roa i.quint_mtb i.quint_salesgrowth if sic_2_n==`i' & year_quarter==`p' if c(rc)==0 { // everything is okay predict resid if e(N)>=10, resid replace absmjda1=resid if sic_2_n==`i' & year_quarter==`p' drop resid } else if !inlist(c(rc), 2000, 2001) { // Unexpected error display as error "Unexpected error in regression" exit c(rc) } } }
Code:
* Example generated by -dataex-. For more info, type help dataex clear input long gvkey_n float(sic_2_n year_quarter acc_1 acc_2 adj_deltasales inverseassets ppe) byte(quint_roa quint_mtb quint_salesgrowth) float absmjda1 1003 57 108 . . . .06855889 . 5 3 4 . 1003 57 109 . . . .06364157 . 2 3 3 . 1003 57 110 . . . .05931902 . 2 3 2 . 1003 57 111 .0519323 .02212466 . .05776674 .13269019 2 3 2 . 1003 57 112 .025620246 .01732951 . .06233637 . 2 2 1 . 1003 57 113 .02885192 .013471503 . .0545405 . 1 2 2 . 1003 57 114 .033642884 .005950064 . .05560808 . 1 3 2 .
Code:
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 > 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 > 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 1 > 49 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 16 > 8 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 > 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 > 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 2 > 26 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 24 > 5 246 247 248 249 250 251 no observations r(2000);
Comment