# R 3.4.1 require(moments) require(tseries) #################################### ### script for basic statistics ### #################################### basic_stats <- function(ts0) { ts <- as.numeric(ts0) hh <- c("Basic Statistics: "," Nobs=", formatC( length(ts), digits = 5, width=8) ) print.noquote(hh) hh <- c("Min= ", formatC( min(ts), digits = 5, width= 8, format = "fg") , "Max= ", formatC( max(ts), digits = 5, width= 8, format = "fg") ) print.noquote(hh) hh <- c("Mean= ", formatC( mean(ts), digits = 5, width= 8, format = "fg") , "StDev= ", formatC( sd(ts) , digits = 5, width= 8, format = "fg") ) print.noquote(hh ) hh <- c("Skewness=",formatC( skewness(ts), digits = 5, width= 8, format = "fg") , "Kurtosis=",formatC( kurtosis(ts), digits = 5, width= 8, format = "fg") ) print.noquote(hh) jbt <- jarque.bera.test(ts); jb <- as.numeric( jbt[1:3]) hh <- c("Jarque Bera Test:"); print.noquote(hh) cat(" Chi2 =",jb[1], " df =", jb[2], " p-value =", jb[3], "\n") } ##################################### plot_ser_fit_res.ts <- function(ser,model,ser_name,start_year,lab_main="") { ## PLOT - Datenreihe (ser) , Fitted, Residuen: # Period is determined by the model. # If lags you loos obs at the beginning! res <- ts(model$residuals, start= start_year) n_res <- length(res) end_year <- start_year + n_res - 1 observed <- ts(ser, start= start_year, end= end_year) fitted <- ts(model$fitted, start= start_year) null <- ts(rep(0,n_res), start= start_year, end= end_year) max_y <- max(fitted,observed); min_y <- min(fitted, observed) # op <- par(mfrow=c(2,1)) par(mar=c(0, 4, 4, 2) +0.1) plot.ts(observed, ylab= ser_name, xlab="", ylim= c(min_y, max_y) ) par(new=TRUE) plot.ts(fitted, xlab="", ylab="", col="blue", axes=FALSE, ylim= c(min_y, max_y) ) lab_title <- paste("observed and fitted, residuals:",lab_main, sep=" ") title(lab_title) # par(mar=c(5, 4, 2, 2) +0.1) plot.ts(res, col="red") lines(null, xlab="", ylab="", lty=3) par(op) # } #######################################