# ------------------------------------------------------ # Program: TimeSeries2.S # Author: Steven M. Boker # Date: Thu Nov 18 10:32:37 EST 2004 # # Timeseries, State Space and Vector Plots Part 2 # # ------------------------------------------------------ # ------------------------------------------------------ # Create a cyclic time series and # synchronous cross-regression relation tX3 <- sin(seq(0,20, by=.1)) tY3 <- 5 + .5 * tX3 + rnorm(201,mean=0,sd=.05) tX3 <- tX3 + rnorm(201,mean=0,sd=.05) tData3 <- data.frame(x=tX3,y=tY3) summary(tData3) # ------------------------------------------------------ # Create a timeseries plot of tX3 tLen <- length(tX3) graphsheet(height=6.4,width=7.5) plot(c(0,tLen), c(-2,2), type="n" , xlab="Occasion of Measurement", ylab="Score") lines(c(1:tLen), tX3, type="l", lty=1) lines(c(0,tLen), c(0,0), type="l", lty=2) # ------------------------------------------------------ # Create an autocorrelation function plot of tX3 maxTau <- 100 tLen <- length(tX3) tACF3 <- rep(NA, maxTau+1) for (tau in (0:maxTau)) { t1 <- tX3[1:(tLen-tau)] t2 <- tX3[(1+tau):tLen] tSelect <- !is.na(t1) & !is.na(t2) if (length(t1[tSelect]) < 5) next tACF3[tau+1] <- cor(t1[tSelect], t2[tSelect]) } graphsheet(height=6.4,width=7.5) plot(c(0,maxTau), c(-1,1), type="n" , xlab="Lag", ylab="Autocorrelation") lines(c(0:maxTau), tACF3, type="l", lty=1) lines(c(0,maxTau), c(0,0), type="l", lty=2) # ------------------------------------------------------ # Create a State Space Plot of tX3 tau <- 8 tLen <- length(tX3) tDataLag3 <- data.frame(x1=tX3[1:(tLen-tau)], x2=tX3[(1+tau):tLen]) graphsheet(height=6.4,width=6.4) plot(c(-2,2), c(-2,2), type="n" , xlab="X (t)", ylab="X (t+63)") lines(tX3[1:(tLen-tau)], tX3[(1+tau):tLen], type="p") graphsheet(height=6.4,width=6.4) plot(c(-2,2), c(-2,2), type="n" , xlab="X (t)", ylab="X (t+tau)") lines(tX3[1:(tLen-tau)], tX3[(1+tau):tLen], type="l", lty=1) # ------------------------------------------------------ # Write the timeseries matrix to a textfile for input to # a recurrence analysis program. tMatrix <- as.matrix(tData3) write(round(t(tMatrix),4)[1,], "c:/Class/Psych344509/tSineData1.dat", ncolumns=1) # ------------------------------------------------------ # Create a randomized sequence independent variable, a regression # relation and a "Problem" condition. rX <- rep(c(1,2,3,4),50)[order(runif(200,0,1))] rY <- 1 + .8*rX + rnorm(200,mean=0,sd=.5) tSelect1 <- c(F,rX[1:199]==4) tSelect2 <- c(rX[1:199]==4,F) rY[tSelect1] <- rY[tSelect2] rDataFrame <- data.frame(rX, rY) summary(rDataFrame) # ------------------------------------------------------ # Calculate a regression summary(lm(rY~rX, data=rDataFrame)) # ------------------------------------------------------ # Scatterplot the data graphsheet(height=6.4,width=6.4) plot(c(0,5), c(0,5), type="n" , xlab="X (t)", ylab="X (t+tau)") lines(rX, rY, type="p") # ------------------------------------------------------ # Create a timelagged dataframe tau <- 1 tLen <- length(rX) rDataLagged <- data.frame(x1=rX[1:(tLen-tau)], x2=rX[(1+tau):tLen], y1=rY[1:(tLen-tau)], y2=rY[(1+tau):tLen]) # ------------------------------------------------------ # Calculate first-order autocorrelations and crosscorrelations cor(rDataLagged) # ------------------------------------------------------ # Write the timeseries matrix to a textfile for input to # a recurrence analysis program. tMatrix <- as.matrix(rDataLagged) write(round(t(tMatrix),4)[3,], "c:/Class/Psych344509/rDataLagged.dat", ncolumns=dim(tMatrix)[2]) # ------------------------------------------------------ # Create a cyclic time series and # synchronous cross-regression relation tX <- sin(seq(0,20, by=.1)) tY <- 5 + .5 * tX[1:201] + rnorm(201,mean=0,sd=.05) tX <- tX[1:201] + rnorm(201,mean=0,sd=.05) tData <- data.frame(x=tX,y=tY) summary(tData) # ------------------------------------------------------ # Create a timelagged dataframe (3-D Embedding) tau <- 15 tLen <- length(tX) rDataLagged <- data.frame(x1=tX[1:(tLen-(2*tau))], x2=tX[(1+tau):(tLen-tau)], x3=tX[(1+(2*tau)):tLen], y1=tY[1:(tLen-(2*tau))], y2=tY[(1+tau):(tLen-tau)], y3=tY[(1+(2*tau)):tLen]) # ------------------------------------------------------ # Create an cross-correlation function plot of tX and tY maxTau <- 50 tLen <- length(tX) tCCF1 <- rep(NA, maxTau+1) for (tau in (-maxTau:maxTau)) { t1 <- tX[(1+maxTau):(tLen-maxTau-tau)] t2 <- tY[(1+maxTau+tau):(tLen-maxTau)] tSelect <- !is.na(t1) & !is.na(t2) if (length(t1[tSelect]) < 5) next tCCF1[maxTau+tau+1] <- cor(t1[tSelect], t2[tSelect]) } graphsheet(height=6.4,width=7.5) plot(c(-maxTau,maxTau), c(-1,1), type="n" , xlab="Lag", ylab="Cross-correlation") lines(c(-maxTau:maxTau), tCCF1, type="l", lty=1) lines(c(-maxTau,maxTau), c(0,0), type="l", lty=2) lines(c(0,0), c(-1,1), type="l", lty=2) # ------------------------------------------------------ # Create a cyclic time series and # synchronous cross-regression relation tX <- sin(seq(0,20, by=.1)) tY <- 5 + .5 * tX[21:201] + rnorm(181,mean=0,sd=.05) tX <- tX[1:181] + rnorm(181,mean=0,sd=.05) tData <- data.frame(x=tX,y=tY) summary(tData) # ------------------------------------------------------ # Write the timeseries matrix to a textfile for input to # a recurrence analysis program. write(tY, "c:/Class/Psych344509/CrossSine1.dat", ncolumns=1) # ------------------------------------------------------ # Quit the program