R CODES FOR NONPARAMETRIC TIME SERIES MODEL By Dr JOHN MUTEBA MWAMBA DEPARTMENT OF ECONOMICS AND ECONOMETRICS UNIVERSITY OF JOHANNESBURG >library(Rcmdr)##import data via Rcmdr package; the data is NOT as.timeSeries >library(np)# >fhat<-npcdens(y~x1,data=Dataset)#visualize the kernel function of two observed ##variables y and x; >summary(fhat) >plot(fhat,view=”fixed”,main=””,theta=300,phi=50)#Retype the inverted commas ##View the cumulative function of the kernel; >plot(fhat, cdf=TRUE,view=”fixed”,main=””,theta=300,phi=50) ##NONPARAMETRIC REGRESSION: KERNEL REGRESSION ## your data=Dataset contains dependent variable y, and independent variables x1, x2, ##x3 and x4. We divide this sample data into two subsamples: the in-sample with size ##n1 and the out-sample with size n2; for example n1=400 and n2=21, the total sample ##size is n=n1+n2=421. >set.seed(123) #creating the in-sample and out-sample data >n1=400 >n2=21 >whol<-sample(1:nrow(Dataset)) 1 >insample1<-Dataset[whol[1:n1],] >outsample<-Dataset[whol[n1+1:nrow(Dataset)],] ;#input the sum n1+1 not the formula #by default, the npreg will result in Nadaraya-Watson estimators in this case type: >modelnp<-npreg(y~x1+x2+x3+x4,data=Dataset) #if you want the local linear then: #get the data-driven bandwidth ##>bwselect<-npregbw(y~x1+x2+x3+x4, regtype=”ll”,bwmethod=”cv.aic”,data=Dataset) ##> modelnp<-npreg(bws=bwselect) ##>summary(modelnp) >summary(modelnp) #to see the regression results >plot(modelnp) ##test of significance >npsigtest(modelnp) ##FORECAST >focastnp<-predict(modelnp,newdata=outsample) >pmse1<-mean((outsample$y-focastnp)^2) >pmse1 ##this is the predicted mean square error of the nonparametric model ##To compare the nonparametric model with (linear) parametric model ##First compute the linear model >modellin<-lm(y~x1+x2+x3+x4,data=Dataset) >summary(modellin) ##Plot the linear model (modellin), but not important >plot(modellin) ##Now compute the forecast for the linear model >focastlin<-predict(modellin,newdata=outsample) 2 >pmse2<-mean((outsample$y-focastlin)^2) >pmse2 ##this is the predicted mean square error for the linear model ##NB: the best model will have the smallest predicted mean square error value ###DIEBOLD MARINO TEST (dm.test) for two econometric models; you must load the ##package “forecast” >library(forecast) ##the dm.test need residuals from the two econometric models >errorlin<-residuals(modellin) >errornp<-residuals(modelnp) > dm.test(errornp,errorlin,alternative=(”two.sided”,”less”,”greater”),h=1,power=2) ###Notice that alternative must be one of “two.sided” or “less” or “greater” and that h is the ##forecast horizon, h=1 means one step ahead forecast, h=2 means two step ahead forecast, ##h=3… ##NB: If you are not using out-sample forecasts then you can use the following code to obtain dm.test: > dm.test(residuals(modelnp),residuals(modellin), alternative=(”two.sided”,”less”,”greater”),h=1,power=2) 3