# Prediction and residual values analysis

Below we demonstrate how to extract prediction and residual values from the different types of regression analyzes. The use of histogram and hexbine is demonstrated for analyzes of the results. Especially `histogram` is a very useful command that can be used to study visually the extent to which the residuals are normally distributed. But in principle, all available and relevant commands may be used for further analyzes.

``````require no.ssb.fdb:1 as fdb1
create-dataset ivtest
import fdb1/INNTEKT_WLONN 2013-05-05 as salary
import fdb1/BEFOLKNING_FOEDSELS_AAR_MND as birth
import fdb1/BEFOLKNING_KJOENN as sex
generate age = 2013 - int(birth /100)
drop if age < 0
generate male = 0
replace male = 1 if sex == '1'
import fdb1/INNTEKT_BRUTTOFORM 2013-05-05 as wealth

//regress
regress salary age male wealth
regress-predict salary age male wealth
histogram salary
histogram predicted
hexbin predicted salary
regress-predict salary age male wealth, residuals( res) predicted( pred) cooksd( cook)
regress-predict salary age male, residuals( res2) predicted( pred2) cooksd( cook2)
histogram predicted
histogram pred
histogram res
histogram cook
histogram res2

//ivregress
ivregress salary male (wealth = age)
ivregress-predict salary male (wealth = age), residuals( res3) predicted( pred3)
histogram pred3
histogram res3

//logit
generate highsalary = 0
replace highsalary = 1 if salary > 600000
logit highsalary age male wealth
logit-predict highsalary age male wealth, residuals( res4) predicted( pred4) probabilities(prob4)
histogram pred4
histogram res4
histogram prob4

//probit
probit highsalary age male wealth
probit-predict highsalary age male wealth, predicted( pred5) probabilities(prob5)
histogram pred5
histogram prob5

//mlogit
generate salarycat = 0
replace salarycat = 1 if salary > 0
replace salarycat = 2 if salary > 600000

mlogit salarycat age male wealth
mlogit-predict salarycat age male wealth, predicted( pred6) probabilities(prob6)
summarize pred6_1
histogram pred6_2
histogram prob6_1
histogram prob6_2

//regress-panel
clone-units ivtest panelpredict
sample 0.2 77777
use panelpredict
import-panel fdb1/INNTEKT_WLONN fdb1/BEFOLKNING_FOEDSELS_AAR_MND fdb1/BEFOLKNING_KJOENN fdb1/INNTEKT_BRUTTOFORM 2011-05-05 2012-05-05 2013-05-05
generate age = 2013 - int(BEFOLKNING_FOEDSELS_AAR_MND/100)
generate male = 0
replace male = 1 if BEFOLKNING_KJOENN == '1'

regress-panel INNTEKT_WLONN male age INNTEKT_BRUTTOFORM
regress-panel INNTEKT_WLONN male age INNTEKT_BRUTTOFORM, re
regress-panel-predict INNTEKT_WLONN male age INNTEKT_BRUTTOFORM, predicted(ppred1) residuals(pres1) effects(peff1)
regress-panel-predict INNTEKT_WLONN male age INNTEKT_BRUTTOFORM, re predicted(ppred2) residuals(pres2) effects(peff2)
histogram ppred1
histogram pres1
histogram peff1
histogram ppred2
histogram pres2
histogram peff2
hausman INNTEKT_WLONN male age INNTEKT_BRUTTOFORM
``````