non-numeric argument to binary operator (but argument IS numeric indeed) - r

The dataset contains stats of teams in MLB matches. All of them are numeric.
best <- lm(mlb11$runs ~ mlb11$bat_avg )
summary(best)
plot(mlb11$bat_avg, mlb11$runs) +
abline(best)
qqnorm(resid(best)) +
qqline(resid(best))
class(resid(best))
The qqnorm and qqline just never worked when connected with the "+".
The last line of code returns "numeric", so I think this error message makes no sense. I actually followed every step on a tutorial but could not add the line to the graph. This is so strange!
follow up:
If I removed the "+" between qqline and qqnorm, it returned "Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) : plot.new has not been called yet"
follow up:
when I entered
qqnorm(resid(best)) +
qqline(resid(best))
The right graph did show up in my R window. However, when I tried to knit the rmd file, it stopped on that line and showed the error message. So weird. Would this be a software/system problem?

Related

ggplot2 in R -- error message and then also no points being plotted

I am working on my data analytics cert (google) on Coursera and there is a hands on activity with ggplot.
It tells me to type this:
ggplot(data = penguins) + geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))
into the console, so I did and got this error message:
Error in draw_axis(break_positions = guide$key[[aesthetic]], break_labels = guide$key$.label, :
lazy-load database '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/4.0/gtable/R/gtable.rdb' is corrupt
In addition: Warning messages:
1: Removed 2 rows containing missing values (geom_point).
2: In draw_axis(break_positions = guide$key[[aesthetic]], break_labels = guide$key$.label, :
restarting interrupted promise evaluation
3: In draw_axis(break_positions = guide$key[[aesthetic]], break_labels = guide$key$.label, :
internal error -3 in R_decompress1
When I then put quote marks around the column names, I at least got what looks like a plot, but absolutely no points on the graph. I tried with different col names, because maybe something isn't a number? but that didn't matter, nothing happens with the plot.
Coursera is not really a whole lot of help, and I've been trying to get help from others in the course (since I'm taking it thru an organization) but haven't been able to get any help. Is there someone out there who sees what I'm doing wrong?

Adding data labels to boxplot in R

Here's my code:
iFacVector <- as.factor(c(1,1,1,1,10,1,1,1,12,9,9,1,10,12,1,9,5))
iTargetVector <- c(2,1,0,1,6,9,15,1,8,0,1,2,1,1,9,12,1)
bp <- plot(iFacVector,iTargetVector)
text(bp,tapply(iTargetVector,iFacVector,median),labels=tapply(iTargetVector,iFacVector,median),cex=.8)
I am getting the following (classic R) error:
Error in xy.coords(x, y, recycle = TRUE) :
(list) object cannot be coerced to type 'double'
The vectors I am passing are numeric so I don't know what the problem is. I have tried unlist() and as.vector(). I have also tried using bp$stats[3,] as the labels.
The help for text gives the arguments as
text(x, ...)
so the first argument in your code, bp, is being treated as the x coordinate for where to place the text. You can just leave off the bp and get better behavior. You might also want to add pos=3 to get a nicer placement of the text.

How does the curve function in R work? - Example of curve function

How does the following code work? I got the example when I was reading the help line of R ?curve. But i have not understood this.
for(ll in c("", "x", "y", "xy"))
curve(log(1+x), 1, 100, log = ll,
sub = paste("log= '", ll, "'", sep = ""))
Particularly , I am accustomed to numeric values as arguments inside the for-loop as,
for(ll in 1:10)
But what is the following command saying:
for(ll in c("","x","y","xy"))
c("","x","y","xy") looks like a string vector? How does c("","x","y","xy") work inside curve
function as log(1+x)[what is x here? the string "x"? in c("","x","y","xy")] and log=ll ?
Apparently, there are no answers on stack overflow about how the curve function in R works and especially about the log argument so this might be a good chance to delve into it a bit more (I liked the question btw):
First of all the easy part:
c("","x","y","xy") is a string vector or more formally a character vector.
for(ll in c("","x","y","xy")) will start a loop of 4 iterations and each time ll will be '','x','y','xy' respectively. Unfortunately, the way this example is built you will only see the last one plotted which is for ll = 'xy'.
Let's dive into the source code of the curve function to answer the rest:
First of all the what does the x represent in log(1+x)?
log(1+x) is a function. x represents a vector of numbers that gets created inside the curve function in the following part (from source code):
x <- exp(seq.int(log(from), log(to), length.out = n)) #if the log argument is 'x' or
x <- seq.int(from, to, length.out = n) #if the log argument is not 'x'
#in our case from and to are 1 and 100 respectively
As long as the n argument is the default the x vector will contain 101 elements. Obviously the x in log(1+x) is totally different to the 'x' in the log argument.
as for y it is always created as (from source code):
y <- eval(expr, envir = ll, enclos = parent.frame()) #where expr is in this case log(1+x), the others are not important to analyse now.
#i.e. you get a y value for each x value on the x vector which was calculated just previously
Second, what is the purpose of the log argument?
The log argument decides which of the x or y axis will be logged. The x-axis if 'x' is the log argument, y-axis if 'y' is the log argument, both axis if 'xy' is the log argument and no log-scale if the log argument is ''.
It needs to be mentioned here that the log of either x or y axis is being calculated in the plot function in the curve function, that is the curve function is only a wrapper for the plot function.
Having said the above this is why if the log argument is 'x' (see above) the exponential of the log values of the vector x are calculated so that they will return to the logged ones inside the plot function.
P.S. the source code for the curve function can be seen with typing graphics::curve on the console.
I hope this makes a bit of sense now!

Error ploting sequences in TraMineR R

Just starting to experiment TraMiner, after having read the (very good) User Guide
I managed to create the sequences from my data, but as I'm trying to plot I got the following errors:
> seqiplot(my.data$sequences, title="My first sequences", withlegend = TRUE)
Error in do.call(plot, args = plist) :
'what' must be a string or a function
Where does this come from and what can I do about it?
I think you get an error because you override the plot function. This reproduce the error:
plot <- 1
do.call(plot,list(0))
Error in do.call(plot, list(0)) :
'what' must be a character string or a function
This should ork:
rm(plot)
seqiplot(my.data$sequences, title="My first sequences", withlegend = TRUE)
I guess the error shows up because you restricted your sequence state object to the single variable my.data$sequences. Have you tried my.data instead?

r - Add text to each lattice histogram with panel.text but has error "object x is missing"

In the following R code, I try to create 30 histograms for the variable allowed.clean by the factor zip_cpt(which has 30 levels).
For each of these histograms, I also want to add mean and sample size--they need to be calculated for each level of the factor zip_cpt. So I used panel.text to do this.
After I run this code, I had error message inside each histogram which reads "Error using packet 21..."x" is missing, with..." (I am not able to read the whole error message because they don't show up in whole). I guess there's something wrong with the object x. Is it because mean(x) and length(x) don't actually apply to the data at each level of the factor zip_cpt?
I appreciate any help!
histogram(~allowed.clean|zip_cpt,data=cpt.IC_CAB1,
type='density',
nint=100,
breaks=NULL,
layout=c(10,3),
scales= list(y=list(relation="free"),
x=list(relation="free")),
panel=function(x,...) {
mean.values <-mean(x)
sample.n <- length(x)
panel.text(lab=paste("Sample size = ",sample.n))
panel.text(lab=paste("Mean = ",mean.values))
panel.histogram(x,col="pink", ...)
panel.mathdensity(dmath=dnorm, col="black",args=list(mean=mean(x, na.rm = TRUE),sd=sd(x, na.rm = TRUE)), ...)})
A discussion I found online is helpful for adding customized text (e.g., basic statistics) on each of the histograms:
https://stat.ethz.ch/pipermail/r-help/2007-March/126842.html

Resources