smpl 1 100 ' check to see if the independent variable, advertising expenditures, is stationary in levels or first differences ident ads ' a first difference of ads is requires for stationarity so a first difference must also be taken in sales genr dads=d(ads) genr dsales=d(sales) ' identify and estimate the best ARIMA model for dads equation pwx.ls dads ar(1) ma(1) ' initialize the prewhitened series as 0 in the periods prior to the beginning of estimated values (note that a difference plus an AR(1) means that the estimated values begin in period 3) smpl 1 2 genr pwads=0 genr fdsales=0 'calculate the prewhitened values of the variables smpl 3 100 genr pwads=dads-c(1)*dads(-1)-c(2)*pwads(-1) genr fdsales=dsales-c(1)*dsales(-1)-c(2)*fdsales(-1) 'use the cross correleations of the prewhitened series to identify the lenght of the period before ads effects sales and the duration of the effect cross pwads fdsales 'use the autocorrelations of the dependent variable to see how many lags to include ident dsales 'estimate the preliminary parameters of the identified transfer function equation nonoise.ls dsales sales(-1) sales(-2) sales(-3) dads(-2) dads(-3) dads(-4) 'obtain the residuals which are the "noise" model genr noise=resid 'identify and estimate the best ARIMA model for the noise series ident noise equation noiseeq.ls noise ma(1) ar(3) 'combine the transfer function and noise models in a single model and estimate its parameters equation wnoise.ls dsales sales(-1) sales(-2) sales(-3) dads(-2) dads(-3) dads(-4) ma(1) ar(3) 'check the residuals to be sure that the moise model is still best 'recover the fitted values of sales and calcualte an R-square based on the level data fit fitdsale 'note that with the various lags and ar terms, the first predicted values will not be obtained until period 9. Set the lagged value of the predicted level equal to the actual value to start the process of obtaining fitted values smpl 8 8 genr fitsales=sales smpl 9 100 genr fitsales=fitsales(-1)+fitdsale genr transerr=sales-fitsales genr tss=@sumsq(sales-@mean(sales)) genr ess=@sumsq(transerr-@mean(transerr)) genr rsqtrans=1-ess/tss 'compare the results for the transfer function with what might have been obtained in a more naive model equation naive.LS SALES C ADS AR(1) MA(1) AR(2) MA(2) MA(5)