2

My first post so apologies for any mistakes.

I'm attempting to perform spectral analysis on a simulated circadian data set. I'm splitting the dataset into overlapping windows of say 72 hours, moving the window by 1 hour at a time then performing analysis on each window.

I've split the data into windows easy enough and the windows are analysed in MATLAB by spectrum resampling before importing back into R to perform the fit and forecast. The data is linearly detrended before analysing.

The problem I have is that sometimes, the fit is upside down and I have no idea why. It seems to be at different points for different simulations.

Data for one window that is guilty of flipping the harmonic fit:

[1]  -2.9136538   0.0010000  34.5624105  22.4405751  30.9085542  52.0034490  79.5249172 
119.5003560 131.3097901 161.3732205 151.1808213 151.3683942 137.2054086 129.6240755 
119.3947248 104.8470942 109.6584816  92.4747798  64.1060229  47.8765937  38.5499292
27.9335235  29.4226107  27.2898893  19.2395761   6.7445437   5.5157589   0.6936448
8.1536902   2.7837173  18.8406092  38.0873956  34.7811886  34.8339832  77.8551701
96.4206791  59.8705545  69.8435641  84.0060386  86.8470648  75.5799761  95.7528280
104.4698246 109.9925047 111.0268326 114.4968343  92.4072921  82.8064504  87.8407758
82.2552400  58.5038630  45.4751850  44.4046889  42.6263098  34.6088522  36.9155973
32.8585151  19.1018107   7.7472503  13.6565334   9.6832063   2.3193501   8.2114646
8.9220096  15.9007696  24.4889114  38.9416853  44.6872649  66.7847050  88.5166601
123.3687771 135.0492302

For this particular window, the significant frequencies are as follows:

Period    Amplitude   Phase
32.508896 52.346609  0.840978
11.882840 17.036845  0.733279
8.650918  6.771955 -0.573897
3.600676  8.561309  0.863454
6.661385  7.278945 -0.823185

Now, the number of frequencies that are picked up differ by window. This is code I've written to produce the sum of the cosine curves and forecasts for each window and put them in a matrix with each column being the fit for that window:

no.freq72_3 <- tabulate(freq72_3$Window)
cusum72_3 <- cumsum(no.freq72_3)    

length <- 28
forecast72_3 <- matrix(NA, nrow=(dim(window72_3)[1])+length,     
ncol=length(no.freq72_3))

for(i in 2:length(no.freq72_3)){
tt72_3 <- matrix(NA, nrow=(dim(window72_3)[1])+length, ncol=no.freq72_3[i])
t72_3 <- c()
a72_3 <- c()
ph72_3 <- c()

for(j in 1:no.freq72_3[i]){
t72_3[j]  <- freq72_3[j+cusum72_3[i-1],2]
a72_3[j]  <- freq72_3[j+cusum72_3[i-1],3]
ph72_3[j] <- freq72_3[j+cusum72_3[i-1],4]
}

for(j in 1:no.freq72_3[i]){
for(l in 1:((dim(window72_3)[1])+length)){
tt72_3[l,j] <- a72_3[j]*cos((2*pi*(1/t72_3[j])*l)+ph72_3[j])
}
}

forecast72_3[,i] <- rowSums(tt72_3, na.rm=TRUE)

}

Now, this is the function I've written to plot whatever window you require:

fore_win72_3 <- function(x){
data <- window72_3[, x]
fore <- forecast72_3[, x]

trend <- time(data)

reg1 <- lm(data ~ trend, na.action=NULL)
detrend <- data - fitted(reg1)
plot(detrend, xlim=c(0, dim(forecast72_3)[1]))
lines(fore)
}

fore_win72_3(100)

I would add plots but I do not have enough reputation. Any help would be greatly appreciated! If there are any edits I can do to make anything easier than let me know! Thanks.

outb4break
  • 43
  • 5

0 Answers0