Histogram: 'x' must be numeric - r

I am currently working on a statical analysis using histogram. However, when I try to plot the histogram of the data, an "x must be numeric" error appeared.
x < - c(1, 2, 3, 4, 5)
y < - c(1, 5, 7, 21, 34)
DATA <- data.frame(x,y)
plot(hist(y ~ x, DATA, breaks = 50))
I try different solutions found on the Stack Overflow community, yet nothing works for my codes. Any help is highly appreciated. Thank You.

Related

Is it possible to extract the value of a function of multiple points using R?

Hello and thanks for reading!
I built an ecdf function using rstudio and I am trying to get the probabilities that correspond to certain points (list format). My R code looks like this:
input <- read.table('/home/agalvez/data/domains/test_ecdf.txt', sep="\t", header=TRUE)
#Build the function
myecdf <- ecdf(input$X118.4)
#Plot the function
plot(myecdf, main = "CDF", xlab = "Bit score", ylab = "Probability") +
abline(v = 25, col = "red")
#Extract probabilities
myecdf(100)
As you can see I am able to extract the probability of one point at a time. But if I try to get more than one I get an error message. I will now attach things that I have tried that did not work.
myecdf(100, 2, 4, ...)
Error in myecdf(100, 2, 4) : unused arguments (2, 4)
myecdf(100)
myecdf(2)
myecdf(4)
...
This approach gives me the results like this in the console:
> myecdf(100)
[1] 0.8048582
> myecdf(2)
[1] 1.382514e-05
> myecdf(4)
[1] 0.0005875684
I would like them to be a list.
myecdf(100,2,4,...)
Error in myecdf(100, 2, 4) : unused arguments (2, 4)
Any help would be really appreciated, sorry for disturbing with this simple question but I am pretty new to R and thanks in advance!
You have to define the wanted values as vector:
myecdf(c(100, 2, 4))

What am I doing wrong with dygraph's showZeroValues option?

I'm trying to plot several data series onto the same plot in R, but even with the showZeroValues=TRUE argument in dyLegend(), the legend stops showing values on mouseover when at least one of the series has a y=0 at the current x. I am not sure what I am doing wrong.
Below is a simplified example:
library(dygraphs)
library(xts)
x=data.frame(a=c(1, 2, 3, 1, 0, 0, 2), b=c(2, 3, 1, 0, 1, 4, 5))
x$Date=seq(as.Date("2017-06-01"), (as.Date("2017-06-01")+dim(x)[1]-1), by="days")
d=xts(x, order.by=x$Date)[,1:2]
dygraph(d) %>%
dyOptions(drawGrid=FALSE, fillGraph=TRUE) %>%
dyLegend(labelsSeparateLines=TRUE, showZeroValues=TRUE)
On my computer the dynamic legend skips all x values at which one of the two series has y=0, as can be seen with the cursor being close to zeros but the legend still stuck on the right end of the graph: example.
I had the same issue and found out that it was caused by the xts object containing character strings. The original data frame had a Date column, which I used to create the xts object, but I did not subset the numerical data. This resulted in the xts object being created but with character values (see issue here). Surprisingly enough, the resulting plots were not much impacted, and the output was correct, which made troubleshooting less straightforward.
In your example, the following should solve the issue:
x=data.frame(a=c(1, 2, 3, 1, 0, 0, 2), b=c(2, 3, 1, 0, 1, 4, 5))
x$Date=seq(as.Date("2017-06-01"), (as.Date("2017-06-01")+dim(x)[1]-1), by="days")
d=xts(x[, 1:2], order.by=x$Date) # This is the only change in your code
dygraph(d) %>%
dyOptions(drawGrid=FALSE, fillGraph=TRUE) %>%
dyLegend(labelsSeparateLines=TRUE, showZeroValues=TRUE)

ttestbf: Error in is.infinite(c(x, y))

This is a question about an error in R's package BayesFactors.
When I tried the code generated in this example, I can use ttestBF fine.
However, when I tried running ttestBF on my data I got the following error:
Error in is.infinite(c(x, y)) :
default method not implemented for type 'list'
I also get that error if I create very simple data like this:
why <- data.frame(sensitivity = c(5, 6, 7, 7, 8, 9), perspective = c(1, 1, 1, 0, 0, 0))
ttestBF(sensitivity ~ perspective, data = why)
t.test(sensitivity ~ perspective, data = why)
The t.test works fine. I imagine it is a very simple issue, but I can't figure this out, and I guess others might face similar issues.
I eventually got the answer from the relevant github Issues thread.
BayesFactor::ttestBF(formula=sensitivity ~ perspective, data = why)

ggplot heatmap failing to fill tiles

This (minimal, self-contained) example is broken:
require(ggplot2)
min_input = c(1, 1, 1, 2, 2, 2, 4, 4, 4)
input_range = c(4, 470, 1003, 4, 470, 1003, 4, 470, 1003)
density = c(
1.875000e-01,
5.598958e-04,
0.000000e+00,
1.250000e-02,
3.841146e-04,
0.000000e+00,
1.250000e-02,
1.855469e-04,
0.000000e+00)
df = data.frame(min_input, input_range, density)
pdf(file='problemspace.pdf')
ggplot(df, aes(x=min_input, y=input_range, fill=density)) +
geom_tile()
dev.off()
Producing:
Why are there big gaps?
There are gaps because you don't have data for all of the tiles. If you want to try to fill them in, your only option is to interpolate (assuming you don't have access to additional data). In theory, geom_raster() (a close relative of geom_tile()) supports interpolation. However, according to this github issue, that feature is not currently functional.
As a workaround, however, you can use qplot, which is just a wrapper around ggplot:
qplot(min_input, input_range, data=df, geom="raster", fill=density, interpolate=TRUE)
If there is too much space between the points that you have data for, you will still end up with blank spaces in your graph, but this will extend the range that you can estimate values for.
EDIT:
Based on the example that you posted, this will be the output
As you can see, there is a vertical band of white running through the middle, due to the lack of data points between 2 and 4.

How do I Order Boxes on One boxplot graph? (r)

I've read the answers to several questions of this type, so sorry for the repeat, but I had trouble understanding how they applied to how my data is laid out.
What I would like is to order the boxes on my boxplot (which has, say, four boxes) in the order I wish as opposed to alphabetically.
This is a simplified version of my current code:
TotalPer = c(1, 4, 6, 17, 4, 12)
IntPer = c(3, 8, 10, 1, 4, 8)
DomPer = c(4, 5, 10, 20, 13, 12)
IntDomBox <- data.frame(y=c(TotalPer,IntPer,DomPer),
x=c(rep("Total",length(TotalPer)),rep("International",length(IntPer)),
rep("Domestic",length(DomPer))))
with(IntDomBox, boxplot(y~x, main = "Prediction Residuals", ylab="%",
par(cex.axis=0.7)))
This produces a boxplot in alphabetical order. If I prefer to have the boxplot in the order listed in the dataframe (Total, Int, Dom) how would I do this? I know about making it reverse alphabetical instead, but in some cases that is not what I want, so I'd prefer to be able to manually assign the order.
Thanks!
One possibility out of many: Reorder the factor levels before plotting by executing IntDomBox$x <- factor(IntDomBox$x, levels=unique(IntDomBox$x))

Resources