# Accuracy Analysis ```{r include=FALSE} library(data.table) library(forecast) require(data.table) require(TSA) require(gridExtra) require(ggplot2) #Get Data pathToFiles <- '../input/' numberOfRowsToRead <- -1 set.seed(55446) dt.train <- read.table(paste0(pathToFiles, "train.csv"), header=T, sep=",",nrows=numberOfRowsToRead) dt.train <- as.data.table(dt.train) dt.train <- dt.train[order(time)] dt.train[,accuracy:=as.numeric(accuracy)] dt.train.subset.accuracy <- dt.train[,list(accuracy=median(accuracy)),by=time] rm(dt.train) ``` The **accuracy** itself is an interesting variable that oscillates in several frequencies. Its values are extremely centered even if its outliers magnitude is quite high. ```{r echo=FALSE,warning=FALSE} acLineplot <- ggplot(data=dt.train.subset.accuracy[1:3000], aes(x=time, y=accuracy, colour="orange")) + geom_line()+ geom_point()+ theme(legend.position="none") acBoxPlot <- ggplot(data=dt.train.subset.accuracy, aes(x=time, y=accuracy)) + geom_boxplot() acDensityPlot <- ggplot(dt.train.subset.accuracy, aes(x=accuracy)) + geom_density(fill="white") acLineplot ``` The graphs verify how is the data distributed. ```{r,echo=FALSE,warning=FALSE} grid.arrange(acBoxPlot,acDensityPlot, ncol=2) ``` In order to find periodicity, the spectral analysis shows this: ```{r, warning=FALSE,echo=FALSE} movingAverage<-60 hour.coef = 60 four.hour.coef<-60*4 day.coef = hour.coef*24 week.coef = day.coef*7 year.coef = day.coef*365 accuracy.sp.pgram <- spec.pgram(scale(dt.train.subset.accuracy$accuracy), spans=(movingAverage*2+1), taper=0, log="no") accuracy.frequencies <- data.table(freq=accuracy.sp.pgram$freq,spec=accuracy.sp.pgram$spec)[order(-spec)] #Calculating period cycles. accuracy.frequencies[,cyclePeriod:=round(1/freq)] #accuracy.frequencies[,cycleHours:=round(cyclePeriod/hour.coef)] accuracy.frequencies[,cycleDays:=round(cyclePeriod/day.coef)] accuracy.frequencies[,cycleWeeks:=round(cyclePeriod/week.coef)] accuracy.frequencies[,cycleMonths:=round(cyclePeriod/(day.coef*30))] #accuracy.frequencies[,cycleYears:=round(cyclePeriod/year.coef,1)] #Selecting frequencies up to 1/500 of max density power. accuracy.frequencies <- accuracy.frequencies[ accuracy.frequencies$spec>max(accuracy.frequencies$spec)/250][1:25] data.frame(accuracy.frequencies[1:nrow(accuracy.frequencies)]) ``` There is a monthly component that defines the strongest frequencies around the following amplitudes in **months**: ```{r, echo=FALSE} bpData<-accuracy.frequencies[,list(power=max(round(spec))),by=list(factor(cycleMonths))][order(factor)] colnames(bpData) <- c("Names","Heights") barplot(height = as.vector(bpData$Heights),names.arg = bpData$Names ,ylim = c(0,500),ylab = "Power Spectral Density",xlab="Frequency in Months" ,main="Monthly Accuracy Spectral Analysis" ) ``` As per the graph, it seems that the frequency is highly related to monthly, quarterly and biannual periodicities. There is also a notable density of sub-monthly frequency. Let's take a look to the **weekly** densities. ```{r, echo=FALSE} bpData<-accuracy.frequencies[,list(power=max(round(spec))),by=list(factor(cycleWeeks))][order(factor)] colnames(bpData) <- c("Names","Heights") barplot(height = as.vector(bpData$Heights),names.arg = bpData$Names ,ylim = c(0,500),ylab = "Weekly Power Spectral Density",xlab="Frequency in Weeks" ,main="Accuracy Spectral Analysis" ) ``` Where the correspondence in **days** are: ```{r, echo=FALSE} bpData<-accuracy.frequencies[,list(power=max(round(spec))),by=list(factor(cycleDays))][order(factor)] colnames(bpData) <- c("Period","Heights") data.frame(bpData) ``` In conclusion, the spectral analysis shows that accuracy is intimately related to monthly and weekly cycles.