What does negative length vectors in a wireframe plot (lattice package) means? - r

I want to plot a wireframe in R using the lattice package. However, I get the following error message "error using packet 1 negative length vectors are not allowed". The data looks like the following:
> result_mean
experiment alpha beta packet
1 0 1.0 1 3.000000
2 0 1.1 1 2.571429
The command to create the data is the following
png(file=paste("foobar.png"),width=1280, height=1280);
plot <- wireframe(result_mean$packet ~ result_mean$alpha * result_mean$beta,
data=result_mean, scales = list(arrows=FALSE, cex= .45, col = "black", font = 3),
drape = TRUE, colorkey = TRUE, main = "Foo",
col.regions = terrain.colors(100),
screen = list(z = -60, x = -60),
xlab="alpha", ylab="beta", zlab="mean \npackets");
print(plot);
dev.off();
I'm wondering what this error message means and if there is a good way to debug this?
Thanks in advance!

Debugging lattice graphics is a bit difficult because (a) the code is complex and multi-layered and (b) the errors get trapped in a way that makes them hard to intercept. However, you can at least get some way in diagnosing the problem.
First create a minimal example. I suspected that your problem was that your data fall on a single line, so I created data that looked like that:
d <- data.frame(x=c(1,1.1),
y=c(1,1),
z=c(2,3))
library(lattice)
wireframe(z~y*x,data=d)
Now confirm that fully three-dimensional data (data that define a plane) work just fine:
d2 <- data.frame(expand.grid(x=c(1,1.1),
y=c(1,1.1)),
z=1:4)
wireframe(z~y*x,data=d2)
So the question is really -- did you intend to draw a wireframe of two points lying on a line? If so, what did you want to have appear in the plot? You could hack things a little bit to set the y values to differ by a tiny bit -- I tried it, though, and got no wireframe appearing (but no error either).
edit: I did a bit more tracing, with various debug() incantations (and searching the source code of the lattice package and R itself for "negative length") to deduce the following: within a function called lattice:::panel.3dwire, there is a call to a C function wireframePanelCalculations, which you can see at https://r-forge.r-project.org/scm/viewvc.php/pkg/src/threeDplot.c?view=markup&root=lattice
Within this function:
nh = (nx-1) * (ny-1) * ng; /* number of quadrilaterals */
sHeights = PROTECT(allocVector(REALSXP, nh));
In this case nx is zero, so this code is asking R to allocate a negative-length vector, which is where the error comes from.
In this case, though, I think the diagnosis is more useful than the explicit debugging.

Related

Strange blue subtitle when plotting objects from the RandomFields package

Theplot method for RFspatialGridDataFrame outputs a weird blue "subtitle" when passing main or axis label arguments to the plot function.
install.packages("RandomFields")
library(RandomFields)
model <- RPbernoulli(RMexp(scale = 3), threshold = 1)
set.seed(100)
simulation <- RFsimulate(model, x = 1:138, y = 1:74)
plot(simulation, main = "This is an example title")
The following is a screeshot of the output
Strangely enough, this appears to be a feature, as running other example for the RandomFields documentation shows.
Is there any way of not outputting this blue repeated title? I have tried fiddling with other graphical arguments (such as setting legend = F) but the behavior does not change.
I have contacted the maintainers of the RandomFields package, and it appears to be a setting in RFoptions.
By setting RFoptions(grPrintlevel = 0) before the plots, this behavior can be avoided. More information can be found under ?RFoptions in section 10 regarding graphics. The relevant part regarding grPrintlevel reads
grPrintlevel: integer values 0, 1, 2; only relevant when simulations are
plotted. The higher the more text is shown in the plot.
Default: 1.

Using the QQ Plot functionality in ggplot

I'm brand new to R, and have a data frame with 8 columns that has daily changes in interest rates. I can plot QQ plots for data each of the 8 columns using the following code:
par(mfrow = c(2,4))
for(i in 1:length(column_names)){
qqnorm(deltaIR.df[,i],main = column_names[i], pch = 16, cex = .5)
qqline(deltaIR.df[,i],cex = .5)
}
I'd like now to use the stat_qq function in the ggplot2 package to do this more elegantly, but just can't get my arms around the syntax - I keep getting it wrong. Would someone kindly help me translate the above code to use ggplot and allow me to view my 8 QQ plots on one page with an appropriate header? Trying the obvious
ggplot(deltaIR.df) + stat_qq(sample = columns[i])
gets me only an error message
Warning: Ignoring unknown parameters: sample
Error: stat_qq requires the following missing aesthetics: sample
and adding in the aesthetics
ggplot(deltaIR.df, aes(column_names)) + stat_qq()
is no better. The error message just changes to
Error: Aesthetics must be either length 1 or the same as the data (5271)
In short, nothing I have done so far (even with Google's assistance) has got me closer to a solution. May I ask for guidance?

Bandwidth when plotting densities in R

When I plot the density for wind direction using circular package, I get an error. The error is shown below. Can someone explain the bw (bandwidth) that I need for the amount of data?
plot(density(dirCir))
Error in density.circular(dirCir) :
argument "bw" is missing, with no default
This is the actual code that I have.
library (circular)
dir <-c(308,351,330,16,3,346,345,345,287,359,345,358,336,335,346,16,325,354,5,354,322,340,6,278,354,343,261,353,288,8)
dirCir <- circular(dir, units ="degrees", template = "geographics")
mean(dirCir)
var(dirCir)
summary(dirCir)
plot(dirCir)
plot(density(dirCir))
rose.diag(dirCir, main = 'dir Data')
points(dirCir)
As #eipi10 says, bw has to be explicitly chosen. Depending on the kernel that you choose large and small values of this bandwidth parameter may produce spiky density estimates as well as very smooth ones.
Common practice is to try several values and choose the one that seems to describe the data best. However, note that the following functions provide more objective ways of selecting the bw:
# bw.cv.mse.circular(dirCir)
[1] 21.32236
# bw.cv.mse.circular(dirCir, kernel = "wrappednormal")
[1] 16.97266
# bw.cv.ml.circular(dirCir)
[1] 19.71197
# bw.cv.ml.circular(dirCir, kernel = "wrappednormal")
[1] 0.2280636
# bw.nrd.circular(dirCir)
[1] 14.63382
When you run density on an object of class circular, it appears that you have to include a value for bw (bandwidth) explicitly (as the error message indicates). Try this:
plot(density(dirCir, kernel="wrappednormal", bw=0.02), ylim=c(-1,5))
See below for the graph. The ylim range is so that the plot fits inside the plot area without clipping. See the help for density.circular for more info on running the density function on circular objects.

Graphing a polynomial output of calc.poly

I apologize first for bringing what I imagine to be a ridiculously simple problem here, but I have been unable to glean from the help file for package 'polynom' how to solve this problem. For one out of several years, I have two vectors of x (d for day of year) and y (e for an index of egg production) data:
d=c(169,176,183,190,197,204,211,218,225,232,239,246)
e=c(0,0,0.006839425,0.027323127,0.024666883,0.005603878,0.016599262,0.002810977,0.00560387 8,0,0.002810977,0.002810977)
I want to, for each year, use the poly.calc function to create a polynomial function that I can use to interpolate the timing of maximum egg production. I want then to superimpose the function on a plot of the data. To begin, I have no problem with the poly.calc function:
egg1996<-poly.calc(d,e)
egg1996
3216904000 - 173356400*x + 4239900*x^2 - 62124.17*x^3 + 605.9178*x^4 - 4.13053*x^5 +
0.02008226*x^6 - 6.963636e-05*x^7 + 1.687736e-07*x^8
I can then simply
plot(d,e)
But when I try to use the lines function to superimpose the function on the plot, I get confused. The help file states that the output of poly.calc is an object of class polynomial, and so I assume that "egg1996" will be the "x" in:
lines(x, len = 100, xlim = NULL, ylim = NULL, ...)
But I cannot seem to, based on the example listed:
lines (poly.calc( 2:4), lty = 2)
Or based on the arguments:
x an object of class "polynomial".
len size of vector at which evaluations are to be made.
xlim, ylim the range of x and y values with sensible defaults
Come up with a command that successfully graphs the polynomial "egg1996" onto the raw data.
I understand that this question is beneath you folks, but I would be very grateful for a little help. Many thanks.
I don't work with the polynom package, but the resultant data set is on a completely different scale (both X & Y axes) than the first plot() call. If you don't mind having it in two separate panels, this provides both plots for comparison:
library(polynom)
d <- c(169,176,183,190,197,204,211,218,225,232,239,246)
e <- c(0,0,0.006839425,0.027323127,0.024666883,0.005603878,
0.016599262,0.002810977,0.005603878,0,0.002810977,0.002810977)
egg1996 <- poly.calc(d,e)
par(mfrow=c(1,2))
plot(d, e)
plot(egg1996)

Using panel.mathdensity and panel.densityplot in lattice graphics to plot Bayesian prior and posterior

I am trying to plot a Bayesian prior and posterior distribution using lattice graphics. I would like to have both distributions in one panel, for direct comparison.
I've tried different solutions all day, including qqmath but I didn't get them to work. Here's the attempt that has been most successful so far:
# my data
d <- dgamma(seq(from=0.00001,to=0.01,by=0.00001),shape = .1, scale = .01)
# my plot
densityplot(~d,
plot.points=FALSE,
panel = function(x,...) {
panel.densityplot(x,...)
panel.mathdensity(
dmath = dgamma,
args = list(shape = .1, scale=.01)
)
}
)
Even though the code runs through nicely, it doesn't do what I want it to. It plots the posterior (d) but not the prior.
I added stop("foo") to densityplot(...) to stop execution if an error occurs and I searched online for the error message:
Error in eval(substitute(groups), data, environment(formula)) : foo
But there are only a few results and they seem unrelated to me.
So, here's my question: Can anyone help me with this approach to achieve what I want?
I asked a similar question which leads to the same result. I got an answer and it was useful. You can find everything here

Resources