On using the biwavelet package in R - r

Could anyone suggest why the following example code does not work:
require(biwavelet)
t <- seq(1/24, 365, 1/24)
A <- 2
fs <- 1/24
y <- A + sin(2*pi*fs*t)
d = cbind(t,y)
wt.t1 <- wt(d)
plot(wt.t1)
It generates an error stating:
Error in image.default(x$t, yvals, t(zvals), zlim = zlims, ylim = rev(range(yvals)), :
invalid z limits
How would I fix this problem?
Additional:
In response to Gavin Simpsons answer: If I keep the data to only include one frequency but alter the time vector, the code works fine.
require(biwavelet)
A <- 2
fs <- 1/24
y <- A + sin(2*pi*fs*t)
d <- cbind(seq(1,8760), y)
wt.t1 <- wt(d)
plot(wt.t1)

You discovered a bug in the wt.R function (errant parentheses). The bug has been fixed in version 0.12 of the biwavelet package, so both versions of your code above should now work.
Thanks for spotting the error. Please don't hesitate to email the maintainer of the package (i.e., me) about bugs in the future.

I suspect this is due to the fact that you have only a single frequency here and the function isn't set up for that. I can get a plot by adding white noise to y:
require(biwavelet)
t <- seq(1/24, 365, 1/24)
A <- 2
fs <- 1/24
y <- A + sin(2*pi*fs*t)
d <- cbind(t, y + rnorm(length(y))) ## add some white noise to y
wt.t1 <- wt(d)
plot(wt.t1)
You might wish to contact the maintainers to report the issue. I got the plot to do something when I debugged it and reversed zlim so that diff(zlim) was positive, so it might be that the author of the plot() method was making an assumption that doesn't hold in all cases.

Related

Wrong coordinates in R plot (package e1071)

I'm trying to add single point to plot.tune but it ends up in a wrong place.
I'm calling
plot(radial.tune2, color.palette = topo.colors)
points(radial.tune2$best.parameters, pch=20, col='red')
where radial.tune2 is tune object.
The parameters are as follows:
> radial.tune2$best.parameters
cost gamma
52 0.385 0.04125
I think this is due to the fact, that the scale bar is not taken into account (as I re-scaled the plot, the red dot appeared in different places relative to axes).
I looked into documentation, but found nothing about adding points to plots of tune object.
I found this question which seems to have similar origin, but does not address the problem properly.
Edit: here is simplified example of the same problem.
It produces different plot, but with the same shifting-to-the-right property.
library(mlbench)
library(e1071)
data(BreastCancer)
BreastCancer$Id <- NULL
BreastCancer <- na.omit(BreastCancer)
data <- as.data.frame(lapply(BreastCancer, as.numeric))
data$Class <- BreastCancer$Class
names(data) <- names(BreastCancer)
C.range <- seq(0.1, 0.7, by=0.05)
gamma.range <- seq(0.001, 0.30, by=0.05)
set.seed(1)
radial.tune <- tune(svm, train.x=data[,-10], # 10 -> Class
train.y=data[,"Class"],
kernel="radial",
ranges=list(cost=C.range, gamma=gamma.range),
tunecontrol=tune.control(sampling="bootstrap"))
plot(radial.tune, color.palette = topo.colors)
points(radial.tune$best.parameters, pch=20, col='red')
I found out that this is actually problem of filled.countour which is being called under the hood.
See this thread for more info.

Two Dimensional Function 3D Plot

I am attempting to plot a two-dimensional function of x and y below in R. I am not good at coding, so it's possible I'm missing something easy, but I got it to work just fine yesterday. It was displaying the 3D plot perfectly and I was even messing with different colors and point sizes for a while with no issues. I save, exit, and then I open the file today and for some reason I am getting an error. I didn't change anything and I have no clue why it won't work now.
library(rgl)
set.seed(27)
x <- runif(100000,-1,1)
y <- runif(100000,-1,1)
## Two-dimensional function of interest
example <- function(x, y){
f_xy <- x^2 + 2*(y^2) - 0.3*cos(3*pi*x) - 0.4*cos(4*pi*y) + 0.7
return(f_xy)
}
example(x,y)
plot3d(
x = x, y = y, z = example(x,y),
type = "p",
size = 1.3,
xlab = "X",
ylab = "Y",
zlab = "f(x,y)",
)
I get the following error message:
Error in example(x, y) :
dims [product 4] do not match the length of object [100000]
I have narrowed it down to the cosine function being the problem I think. If I mess with
f_xy <- x^2 + 2*(y^2) - 0.3*cos(3*pi*x) - 0.4*cos(4*pi*y) + 0.7
and get rid of the 3pi for x and the 4pi for y then it gives me a plot, but it's obviously not the one I need since I have now changed the equation.
Any ideas on how I can fix this?
I just figured out how to use cospi(3*x) instead of cos(3*pi*x) and now it works. I honestly have no idea why it worked yesterday without me doing this.

Graphing non linear equation of two variables in RStudio

I want to graph 0.2*ln(x/40)-0.02(x-40)=-0.04ln(y/100)+0.004(y-100) in RStudio. I installed 'manipulate' package and used plotFun but it didnt work. I tried using plot.function but I couldnt find a solution. please help
Not sure what you've tried since you didn't provide any code. Perhaps this helps: Solve your equation for z (or set it = 0), then give plotFun your new expression:
library(manipulate)
library(mosaic)
plotFun(0.2*log(x/40)- 0.02*(x-40) + 0.04*log(y/100) - 0.004*(y-100) ~
y+x, surface=TRUE, ylim=c(0,5), xlim=c(0,5))
If you just want for z=0, you can use contour:
xyFun <- function(x,y){
0.2*log(x/40)- 0.02*(x-40)+0.04*log(y/100) - 0.004*(y-100)
}
x <- y <- seq(0,5, by=0.1)
z <- outer(x,y,xyFun)
contour(x,y,z, levels=0)
or just
contour(x,y,z)

Drawing an interval on the graph using vertical line in R ?

install.packages("devtools")
library(devtools)
devtools::install_github("google/CausalImpact")
library(CausalImpact)
set.seed(1)
x1 <- 100 + arima.sim(model = list(ar = 0.999), n = 100)
y <- 1.2 * x1 + rnorm(100)
y[71:100] <- y[71:100] + 10
data <- cbind(y, x1)
pre.period <- c(1, 70)
post.period <- c(71, 100)
impact <- CausalImpact(data, pre.period, post.period)
plot(impact, "cumulative")
Say i want the graph to show an interval from 71-100 with the x scales starting at 1 from the first dotted line any ideas on how to do this?
Does anyone have any idea how to add a second vertical dotted line depicting an interval on the graph? Thanks.
You can use abline() to add lines to a graph, with the argument v = 70 setting a vertical line at x = 70. I'm not sure how to restart the x-scale from that point however - it doesn't seem like something that would be possible but perhaps someone else knows how.
You can reset the axes using this.
In your initial plot command, set xaxt = "n" This ensures that the plot function does not mark the axes.
You can then draw the abline(v=70) as mentioned above.
Then use axis(1,at=seq(60,80,by=1),las=1) 1 stands for x-axis and in the at attribute, mention the x limits you want. I've put in 60 to 80 as an example.

could not find function "ceil" error, when trying to plot histogram

U Aight guys. I am very new to Rstudio only knew about it this week. I am using windows 8 and R studio the newest version and have installed RGui as well.
I am trying to plot a histogram for some data and keep getting this error message, even when I try the code in RGui:
screen(1)
histplot(t(X),"metabolites: Hist")
Error in histplot(t(X), "metabolites: Hist") :
could not find function "ceil"
Why does it ask me for "ceil" what does this mean? I just want to plot my histogram?
My data is represented in 4 rows by 200 columns and in the first column is 4 different names of variables. I.e. each row represents data from a different variable.
I am thinking its because I would need to do a histogram for each row separately rather than trying to do a histogram for all data. Could this be the reason the error is coming up?
I am using the package histplot.r, here is the script for histplot:
histplot <- function(x,sam) {
nn <- ceil(sqrt(length(x)))
BW <- (max(x)-min(x))/nn
xxx <- hist(x, freq = TRUE, main=sam)
N <- xxx$counts # Get the freq distribution
MIDS <- xxx$mids # Get the centres
Xaxis <- seq(min(x), max(x), by = 0.01)
MU <- mean(x)
SIGMA <- std(x)
y <- exp(-0.5 * ((Xaxis - MU)/SIGMA)^2) / (sqrt(2*pi) * SIGMA)
matplot(Xaxis, (y * BW * sum(N)), type="l", col = 2, lty = 1, lwd = 1, add = TRUE)
abline(a=NULL,b=NULL, h=NULL, v=MU, col=3, lty=2, lwd=1) # Add mean
abline(a=NULL,b=NULL, h=NULL, v=MU-SIGMA, col=4, lty=2, lwd=1) # Add mean-stdev
abline(a=NULL,b=NULL, h=NULL, v=MU+SIGMA, col=4, lty=2, lwd=1) # Add mean+stdev
}
The histplot didn't work when I run it in my script in Rstudio, screen 1. Here is my full script:
rm(list=ls())
graphics.off()
source("histplot.r")
source("normplot.r")
X <- read.table("metabolites.csv",sep=",",header=FALSE)
x11()
split.screen(c(1,2))
screen(1)
histplot (t(X), "Metabolites: Hist")
screen(2)
bbb <- qqplot(t(X), ppoints(t(X)), ylab =
"Probability", main = "metabolites: Norm")
qqplot(bbb)
The ceil function is a function that histplot uses under the hood. It is not part of a standard R install, and not part of any of the additional packages you loaded. This leads to the error you get, it cannot find the ceil function. I suspect the ceil function has the same functionality as the ceiling function, but I'm just speculating here.
Probably the ceil function is part of a package on which the histplot package depends. Try reinstalling the package with dependencies = TRUE. From your comment it turned out you needed to also load the matlab package, which contains the ceil function.
I believe the ceil function is in the pracma library
https://www.rdocumentation.org/packages/pracma/versions/1.9.9/topics/ceil

Resources