Below are the solutions to here. exercises on “Seasonality and Trend Decomposition.”

#################### # # # Exercise 1 # # # #################### #***This lines are setup of the the tutorial if(!require(ggplot2)){install.packages(ggplot2, dep=T)} # import datafiles # "C:/yourDir.../PAICOL.csv"#CHANGE the directory where the data is stored river_data <- read.csv("D:/TRABAJO/BLOG/post1/PAICOL.csv") # Convert strings values into dates river_data$DATE=as.Date(river_data$DATE,origin=river_data$DATE[1]) #*** # Create a time series object ts_river = ts(river_data$LEVEL, frequency = 365) #################### # # # Exercise 2 # # # #################### # Multiplicative decomposition decompose_river_m = decompose(ts_river, " multiplicative")

## Error in match.arg(type): 'arg' should be one of "additive", "multiplicative"

plot(decompose_river_m)

#################### # # # Exercise 3 # # # #################### # Additive decomposition decompose_river_a = decompose(ts_river, "additive") plot(decompose_river_a)

#################### # # # Exercise 4 # # # #################### # Reconstruct decomposition compose_river_a=decompose_river_a$seasonal+ decompose_river_a$trend+ decompose_river_a$random plot(ts_river) lines(compose_river_a,col="green")

#################### # # # Exercise 5 # # # #################### # Reconstruct decomposition compose_river_m=decompose_river_m$seasonal* decompose_river_m$trend* decompose_river_m$random plot(ts_river) lines(compose_river_m,col="blue")

#################### # # # Exercise 6 # # # #################### # Estimate and plot the difference d1=decompose_river_m$seasonal-decompose_river_a$seasonal plot(d1)

d2=decompose_river_m$random-decompose_river_a$random plot(d2)

#################### # # # Exercise 7 # # # #################### # Function to estimate the periodogram my.periodogram=function(x){ # calculate the periodogram raw.spec <- spec.pgram(x, taper = 0, plot = FALSE) # Create a dataframe for the periodogram f=raw.spec$freq p=1/f spec=raw.spec$spec # Normalize the periodogram to turn in into p spec=raw.spec$spec/sum(raw.spec$spec) periodogram= data.frame(f,p, spec) return(periodogram) } #################### # # # Exercise 8 # # # #################### # Calculate the periodogram for the seasonal component seasonal_m=my.periodogram(decompose_river_m$seasonal[!is.na(decompose_river_m$seasonal)]) seasonal_a=my.periodogram(decompose_river_a$seasonal[!is.na(decompose_river_a$seasonal)]) # Calculate the periodogram for the random component random_m=my.periodogram(decompose_river_m$random[!is.na(decompose_river_m$random)]) random_a=my.periodogram(decompose_river_a$random[!is.na(decompose_river_a$random)]) #################### # # # Exercise 9 # # # #################### # Create a overlapped plot of the seasonal periodograms ggplot(data=seasonal_m,aes(x = p, y = spec,color="*"))+geom_line()+ coord_trans(x="log",y="log")+ geom_line(data=seasonal_a,aes(x = p, y = spec,color="+"))

# Create a overlapped plot of the random periodograms ggplot(data=random_m,aes(x = p, y = spec,color="*"))+geom_line()+ coord_trans(x="log",y="log")+ geom_line(data=random_a,aes(x = p, y = spec,color="+"))

## Leave a Reply