library(bspec)
library(lomb)
library(WaveletComp)
library(zoo)
library(quantmod)
library(dplyr)
library(astrochron)
library(IRISSeismic)
library(Hmisc)
setwd("D:/Masters in Climate Change/Spectral Analysis/Data")
spec1 <- welchPSD(sunspots, seglength=200)
plot(spec1$frequency, spec1$power, log='y',type="l", xlim=c(0,1))
spec1$frequency[findPeaks(spec1$power, thresh=30000.0)]
## [1] 0.095
read.csv("NAO.csv", header=TRUE)
mydata <- read.table("NAO.csv", header=FALSE, sep=",")[,2]
NAO <- ts(mydata,start=1825, end=2017)
plot (NAO, type="l")
Naowelch <- welchPSD(NAO,seglength = 100)
plot(Naowelch$frequency,Naowelch$power,type="l",xlim=c(0,0.6))
Naowelch$frequency[findPeaks(Naowelch$power, thresh=1)]
## [1] 0.09 0.14 0.21
1/0.09 gives 11.11111, 1/0.14 gives 7.142857, 1/0.21 gives 4.761905,
write.table(NAO, file = "Winter_NAO_data.csv", col.names=FALSE, sep=",")
rm(Naowelch)
rm(NAO)
rm(mydata)
read.csv("Precipitation_Ireland.csv", header=TRUE)
mydata <- read.table("Precipitation_Ireland.csv", header=TRUE, sep=",")
Ireprecip<- ts(mydata,start=1825, end=2017)
plot (Ireprecip, type="l", main='Irish precipitation timeseries')
data2 <- (mydata %>% filter(mydata$Month == c(1, 2)))
data_winter <- aggregate(data2[,3],list(data2$Year), mean)
detrended <- detrend(data_winter)
##
## ----- SUBTRACTING LINEAR TREND FROM STRATIGRAPHIC SERIES -----
## * Slope= 0.07684393
## * y-intercept= -51.38379
dev.off()
## null device
## 1
detrended_ts <- ts(detrended$x,start=1711, end=2017)
plot (detrended_ts, type="l", main='Detrended Irish precipitation timeseries')
detrended_tswelch <- welchPSD(detrended_ts,seglength = 50)
plot(detrended_tswelch$frequency,detrended_tswelch$power,type="l",xlim=c(0,0.6))
detrended_tswelch$frequency[findPeaks(detrended_tswelch$power, thresh=600)]
## [1] 0.04 0.14 0.20 0.30
write.table(detrended_ts, file = "Winter _precipitation_data.csv", row.names=FALSE,
col.names=FALSE, sep=",")
read.csv("Winter _precipitation_data.csv", header=FALSE)
precipitation_winter <- read.table("Winter _precipitation_data.csv", sep=",")
read.csv("Winter_NAO_data.csv", header=FALSE)
NAO_winter <- read.table("winter_NAO_data.csv", sep=",")
ts1<- ts(precipitation_winter$V1,start=1711, end=2016)
ts2<- ts(NAO_winter$V2,start=1825, end=2017)
t <- ts.intersect(ts1,ts2,dframe=TRUE)
correlation = rcorr(t$ts1,t$ts2, type="pearson")
DF <- crossSpectrum(t)
transferFunction <- DF$Pxy / DF$Pxx
transferAmp <- Mod(transferFunction)
plot(DF$freq,transferAmp,type='l',log='x',
xlab="Frequency",
main="Transfer Function Amplitude")
write.table(t, file = "NAO_and_precipitation_data.csv", col.names=FALSE, sep=",")
read.csv("pedrido.csv", header=TRUE)
mydata <- read.table("pedrido.csv", header=TRUE, sep=",")
plot (mydata$Sand~mydata$Age, type="l")
### There are very little cycles to examine, however there are peaks around each interval of 1000 followed by a decrease. ### The amplitude of each peak differs however there is arguably a cylce in the Age axis.
mydata$Depth=NULL
ls<- lsp(mydata, from = NULL, to = NULL, ofac = 5, alpha = 0.05, plot = TRUE)
lsp(mydata, from = NULL, to = NULL, ofac = 5, alpha = 0.05,type = c("period"), plot = TRUE)
New_age <- seq(from = -10, to = 4600, by = 10)
new_data <- approx(x=mydata$Age, y=mydata$Sand, xout=New_age, method="linear")
names(new_data) <- c("date","sand")
plot(mydata$Age, mydata$Sand, type="o", col="blue", lty=1, main="Original data vs Downsampled data")
points(new_data$date, new_data$sand, col="red", pch="*")
lines(new_data$date, new_data$sand, col="red",lty=2)
write.csv(new_data, file = "PedridoEvenData.csv")
Pedrido <- read.csv("PedridoEvenData.csv")[ ,3]
New_age <- read.csv("PedridoEvenData.csv")[ ,2]
my.data = data.frame(date = New_age, Pedrido=Pedrido)
my.wt = analyze.wavelet(my.data, "Pedrido", loess.span = 0, dt = 10, dj = 1/20, make.pval = TRUE,
method = "white.noise", params = NULL, n.sim = 100, date.format = "%Y", date.tz = NULL, verbose =
TRUE)
wt.image(my.wt, color.key = "i", label.time.axis = TRUE, main = "wavelet power spectrum",
legend.params = list(lab = "wavelet power levels"), periodlab = "period (years)", timelab = "time (years)",
date.format = "%d", spec.time.axis = list(at = seq(-10, 462, by = 10), labels = seq(0, 460, by = 50)))
## Warning in wt.image(my.wt, color.key = "i", label.time.axis = TRUE, main = "wavelet power spectrum", :
## Please check your time axis specifications. Default settings were used.
wt.avg(my.wt, siglvl=0.05, sigcol="red")
read.csv("NAO_and_precipitation_data.csv", header=FALSE)
NAO_precip_winter <- read.table("NAO_and_precipitation_data.csv", sep=",")[,2:3]
my.date <- seq(as.POSIXct("1825-01-30 00:00:00", format = "%Y"),
by = "year",
length.out = 192)
date<- my.date
my.data <- cbind(NAO_precip_winter,date)
my.wc <- analyze.coherency(my.data, c(1, 2), dt=1)
wc.image(my.wc, main = "cross-wavelet power spectrum, x over y",
legend.params = list(lab = "cross-wavelet power levels"),
periodlab = "period (years)", show.date = TRUE)
wc.avg(my.wc, siglvl = 0.05, sigcol = 'red',
periodlab = "period (years)")
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.