A calendar issues in wavelet analyze - r

I want to apply a simple wavelet analyze using "waveletcomp" package. I want to use the year shown in x-axis. But it always report error in "lease check your calendar dates, format and time zone: dates may not be in an unambiguous format or chronological. The default numerical axis was used instead." I tried to fix the date, but it seems fine. I really don't know where is the wrong part. Thank you in advance.
Here is the code.
library('WaveletComp')
firecount <- data.frame( YEAR = c("1986-01-01","1987-01-01","1988-01-01","1989-01-01","1990-01-01"
,"1991-01-01","1992-01-01","1993-01-01","1994-01-01","1995-01-01"
,"1996-01-01","1997-01-01","1998-01-01","1999-01-01","2000-01-01"
,"2001-01-01","2002-01-01","2003-01-01","2004-01-01","2005-01-01"
,"2006-01-01","2007-01-01","2008-01-01","2009-01-01","2010-01-01"
,"2011-01-01","2012-01-01","2013-01-01","2014-01-01","2015-01-01"
,"2016-01-01","2017-01-01","2018-01-01","2019-01-01","2020-01-01"
),
COUNT = c(3,5,4,0,0,0,13,0,2,3,0,1,0,3,15,13,
59,18,42,16,20,46,44,8,68,18,7,3,9
,48,7,48,23,84,54)
)
flycount$YEAR <- as.Date(as.character(firecount$YEAR),"%Y")
my.w <- analyze.wavelet(flycount, my.series = "COUNT",
loess.span = 0.5,
dt = 1, dj = 1/35,
lowerPeriod = 2, upperPeriod = 12,
make.pval = TRUE, n.sim = 10,
)
wt.image(my.w, color.key = "interval", n.levels = 15,
legend.params = list(lab = "fire occurrence wavelet", label.digits = 2),
periodlab = "periods (years)",
# Concerning item 1 above --- plot the square root of power:
exponent = 0.5,
# Concerning item 2 above --- time axis:
show.date = TRUE,
date.format = "%F",
timelab = "",
spec.time.axis = list(at = c(paste(1986:2020, "-01-01", sep = "")),
labels = c(1986:2020)),
timetcl = -0.5)

The function analyze.wavelet automatically takes the date from a dataframe column called date. So just rename your column from YEAR to date and you're good to go.

Related

Error while running WTC (Wavelet Coherence) Codes in R

I am doing Wavelet Analysis in R using Biwavelet. However, I receive the error message:
Error in check.datum(y) :
The step size must be constant (see approx function to interpolate)
When I run the following code:
wtc.AB = wtc(t1, t2, nrands = nrands)
Please share your help here. Complete Code is:
# Import your data
Data <- read.csv("https://dl.dropboxusercontent.com/u/18255955/Tutorials/Commodities.csv")
# Attach your data so that you can access variables directly using their
# names
attach(Data)
# Define two sets of variables with time stamps
t1 = cbind(DATE, ISLX)
t2 = cbind(DATE, GOLD)
# Specify the number of iterations. The more, the better (>1000). For the
# purpose of this tutorial, we just set it = 10
nrands = 10
wtc.AB = wtc(t1, t2, nrands = nrands)
# Plotting a graph
par(oma = c(0, 0, 0, 1), mar = c(5, 4, 5, 5) + 0.1)
plot(wtc.AB, plot.phase = TRUE, lty.coi = 1, col.coi = "grey", lwd.coi = 2,
lwd.sig = 2, arrow.lwd = 0.03, arrow.len = 0.12, ylab = "Scale", xlab = "Period",
plot.cb = TRUE, main = "Wavelet Coherence: A vs B")```

twoord.plot axis changing

I am using twoord.plot for the first time, and I am having trouble getting the x axis set to years for a time-series data set. I have two different y-axes on different scales. Here is the code that I am working with.
#Install BatchGetSymbols
install.packages('BatchGetSymbols')
library(BatchGetSymbols)
#Get data from FRED
library(quantmod)
getSymbols('CPALTT01USM661S', src = 'FRED')
library(quantmod)
getSymbols('M2SL', src = 'FRED')
#Create data sets with equal number of observations
CPI = CPALTT01USM661S["1960-01-01/2019-01-01"]
M2 = M2SL["1960-01-01/2019-01-01"]
library(plotrix)
twoord.plot(rx = time(CPI), ry = CPI, lx = time(CPI), ly = M2,
main = "Money Supply and Prices",
xlim = NULL, lylim = NULL, rylim = NULL,
mar = c(5,4,4,4), lcol = "red", rcol = "blue", xlab = "", lytickpos = NA,
ylab = "M2", ylab.at = NA,
rytickpos = NA, rylab = "CPI", rylab.at = NA, lpch = 1,rpch = 2,
type = "l", xtickpos = NULL, xticklab = NULL,
halfwidth = 0.4, axislab.cex = 1, do.first = NULL)
Here is the graph that I am getting. Notice the x-axis is not in years.
The date values ( beginnings of each month) are in the index of the matrices, so to extract the year beginnings get every 12th item:
twoord.plot(rx=time(CPI), ry=CPI, lx=time(CPI),ly = M2, main="Money Supply and Prices",xlim=NULL,lylim=NULL,rylim=NULL,
mar=c(5,4,4,4),lcol="red",rcol="blue",xlab="",lytickpos=NA,ylab="M2",ylab.at=NA,
rytickpos=NA,rylab="CPI",rylab.at=NA,lpch=1,rpch=2,
type="l",
xtickpos=index(CPI)[seq(1,nrow(CPI), by=12)], #tick at year start
xticklab=format( index(CPI)[seq(1,nrow(CPI), by=12)], "%Y"), #just year
halfwidth=0.4, axislab.cex=1,
do.first=NULL, las=2) # not sure why las=2 didn't seem to work.

R igraph Format Date on axis

In the following igraph there are dates to be plotted as marks on the x-axis. Below I provided an example. As the dates are specified in the label matrix they are formatted into an atomic value. How do I get the dates on the x-axis to be displayed in a regular date format?
library(igraph)
nodes=data.frame(
c(0,1,2,3),
c("A","B","C","D")
)
colnames(nodes) = c("id","name")
links = data.frame(
c(0,0,1,2),
c(1,2,3,3)
)
colnames(links) = c("from","to")
layout = matrix(
c(as.Date('2010-01-01'),1, as.Date('2010-01-02'),1, as.Date('2010-01-02'),2, as.Date('2010-01-06'),1), byrow = TRUE, nrow=4
)
net = graph.data.frame(links, vertices = nodes)
plot.igraph(
net, xaxt="n",layout=layout,axes=TRUE,asp=0, rescale=FALSE,xlim=c(as.Date('2010-01-01'),as.Date('2010-01-06')),ylim=c(1,2)
)
You can replace the axis by your own values as explained here.
Using your code, it gives:
layout <- data.frame(Date = as.Date(c('2010-01-01','2010-01-02','2010-01-02','2010-01-06')), value = c(1,2,1,1))
plot.igraph(
net,
layout = layout,
rescale = FALSE,
axis = FALSE,
asp = 0,
xlim = as.Date(c('2010-01-01', '2010-01-06')),
ylim = c(1,2)
)
axis(1, at = as.numeric(layout$Date), labels = layout$Date, cex.axis = 0.9)
axis(2, at = 1:max(layout$value), labels = 1:max(layout$value))

turn off grid lines for R xyplot timeseries

I am plotting a time series with the timePlot function of the open air package of R. The graph has grey grid lines in the background that I would like to turn off but I do not find a way to do it. I would expect something simple such as grid = FALSE, but that is not the case. It appears to be rather complex, requiring the use of extra arguments which are passed to xyplot of the library lattice. I believe the answer lies some where in the par.settings function but all attempts have failed. Does anyone have any suggestions to this issue?
Here is by script:
timeozone <- import(i, date="date", date.format = "%m/%d/%Y", header=TRUE, na.strings="")
ROMO = timePlot(timeozone, pollutant = c("C7", "C9", "C10"), group = TRUE, stack = FALSE,y.relation = "same", date.breaks = 9, lty = c(1,2,3), lwd = c(2, 3, 3), fontsize = 15, cols = c("black", "black"), ylab = "Ozone (ppbv)")
panel = function(x, y) {
panel.grid(h = 0, v = 0)
panel.xyplot(x,y)
}

Why aren't event.lines showing up when using chart.TimeSeries?

I am trying to plot various events over a times series data set using chart.TimeSeries in the PerformanceAnalytics library. I have tried to follow the example from but I think I am missing something.
ExampleChart MyChart
Neither my vertical event lines nor are my period areas are showing up on the chart. My code is almost identical to the example code. Any ideas?
risk.dates = c(
"2015-04-19"
)
risk.labels = c(
"Jon Oliver's Patent Episode "
)
cycle.dates <- c ("2015-1-01/2015-01-31",
"2015-03-01/2015-03-31",
"2015-05-01/2015-05-31",
"2015-07-01/2015-07-31"
)
chart.TimeSeries(df, colorset = "darkblue",
date.format.in = "%Y-%m-%d",
legend.loc = NULL,
period.areas = cycles.dates,
period.color = "lightblue",
event.lines = risk.dates,
event.labels = risk.labels,
event.color = "red", lwd = 2,
main = "Reddit Sentiment Index",
auto.grid = FALSE)
df <- data.frame(DateTime=
c("2/17/2015","2/18/2015","2/19/2015",
"2/20/2015","2/21/2015","2/22/2015",
"2/23/2015","2/24/2015","2/25/2015",
"2/26/2015","2/27/2015","2/28/2015",
"2/28/2015","2/27/2015"),
AvgSentimentScore=
c("NA","NA","NA","NA","NA","NA","NA","NA","NA",
"8.405975696","8.313470536","8.414560335",
"8.720865075","9.284902246"))

Resources