Histogram with density line - r

I want to plot a histogram with density line. The code below does not work. Any idea why?
control <- c(4.17,3.05,5.18,4.01,6.11,4.1,5.17,3.57,5.33,5.59,
4.66,5.58,3.66,4.50,3.90,4.61,5.62,4.53,6.05,5.14)
hist(control,col="gray",xlab="Utility",prob=TRUE,
main="Histogram of individual utilities")
curve(dnorm(control,mean=mean(control),
sd=sd(control)), add=TRUE,col="red")

What was your error message? You should have told us. I get:
Error in curve(dnorm(control, mean = mean(control), sd = sd(control)), :
'expr' must be a function, or a call or an expression containing 'x'
So make it an expression containing x:
curve(dnorm(x,mean=mean(control),sd=sd(control)), add=TRUE,col="red")

Related

Trying to make a histogram with two variables and keep coming up with 'x' must be numeric

I am trying to make a good histogram with two variables, height, and free throw percent. I've​ imported the data using excel and used the hist function but keep coming up with 'x' must be numeric.
I can read the data and create a table.
I've tried hist(height$freethrow)
and hist(shortkings)
This is what my data looks like
Second part of my data
hist(shortkings)
Error in hist.default(shortkings) : 'x' must be numeric
hist(shortkings, xlab = Height, ylab = Freethrow, main = Freethrow)
Error in hist.default(shortkings, xlab = Height, ylab = Freethrow, main = Freethrow) :
'x' must be numeric
I would like to create a histogram that shows distribution.
What happens if you run class(shortkings$Height)
If you see that it's non-numeric, then you can do the following and re-run the hist() function
shortkings$Height <- as.integer(shortkings$Height)

function-expression R problem when using curve()

When plot using curve() in R I give a function as an argument.
E.g.
f=function(x) x^2
curve(f,2,3)
I get the curve plotted.
But I have to work with the derivative function D(), which you have to give an expression as argument and I can't get my curve plotted.
This is my code:
#To get the derivative
f1 = expression((x)^2)
d1=D(f1,"x")
#To plot the curve
f1=function(x) eval(f1,"x")
curve(f1,2,3)
And the error is:
Error in eval(f1, "x") : invalid 'envir' argument of type 'character'
How can I fixed? I've tried to plot directly the expression and no result. I can fix the problem if I were able to convert the function to expression, but no clue either.
Thanks in advance, Alberto.
I think you've mistyped because you want to evaluate d1 and NOT f1
#To get the derivative
f1 = expression((x)^2)
d1=D(f1,"x")
#To plot the curve
f=function(x) eval(f1)
curve(f,2,3)
The code above works for me.

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

How to add median vertical line to panels of lattice density plot?

I'm using a data frame in a long format to create a panelled density plot in lattice. Now I'd like to add a vertical line at the median x-value within each panel. I found a suggestion for doing it in an dotplot ( http://r.789695.n4.nabble.com/how-to-add-a-vertical-line-for-each-panel-in-a-lattice-dotplot-with-log-scale-td4632513.html ), but this doesn't work for me. Here is my code:
data(Chem97, package="mlmRev")
densityplot(~gcsescore | factor(score), data=Chem97,
panel=function(...){
panel.densityplot(...)
median.values <- median(x)
panel.abline(v=median.values, col.line="red")
})
The error is: Object x not found. So I tried the following:
panel=function(x,...){
panel.densityplot(...)
}
The moment I add x as an argument to the panel function, I get the error Error using packet 1 (2, 3 etc.). x is missing.
What's going wrong?
I've finally found the solution:
densityplot(~gcsescore | factor(score), data=Chem97,
panel=function(x,...){
panel.densityplot(x,...)
panel.abline(v=quantile(x,.5), col.line="red")
})

'x' is a list, but does not have components 'x' and 'y'

i am trying to plot a ROC curve for a multiclass problem, using multiclass.roc function from pROC package, but I get this error:
'x' is a list, but does not have components 'x' and 'y'
What does this error mean cause searching in the web didn't help me to find an answer. I can print the roc object, but can not plot it.
Thank you!
If you call plot on a list l: plot (l), the x coordinates will be taken from l$x and the y coordinates from l$y. Your list doesn't have elements x and y.
You need to call plot (l$your.x.coordinate, l$your.y.coordinate) instead.
Another (lazy) approach is to simply use the useful library
install.packages('useful')
library(useful)
Example -
wineUrl <- 'http://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data'
wine <- read.table(wineUrl, header=F, sep=',')
wine_kmeans <- wine[, which(names(wine) != "Cultivar")]
wine_cluster <- kmeans(x=wine_kmeans , centers=3)
plot(wine_cluster, data=wine_kmeans)

Resources