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.
Related
I was reading other similar questions in this site about how to plot a piecewise function in R and I write this code
myfunction = function(x){(x<-1)*(x^2+2*x)+(-1<=x & x<=1)*x+(x>1)*(-1)}
g=Vectorize(myfunction)
plot(g,-5,5)
However the plot is just a straight line, that is, it doesnt plot the defined function. What Im doing wrong?
<- is assignment and won't be interpreted as "less than negative". Add a space in there and you'll see what you expect.
myfunction = function(x){(x< -1)*(x^2+2*x)+(-1<=x & x<=1)*x+(x>1)*(-1)}
Basically, i have a dataframe with 3 numeric vectors(x,y,z), and lets say i wanna make a scatter plot of x,y colored by z. I want to transform the colorscale with a squareroot that respects sign, so i made my own with trans_new. Here is a simple dataset, but with the actual transform.
library(ggplot2)
library(scales)
set.seed(1)
plot<-data.frame(x=rnorm(100),y=rnorm(100),z=rnorm(100))
super_trans <- function(){
trans_new('super', function(X) sapply(X,function(x) {if(x>0){x^0.5} else{-(- x)^0.5}}), function(X) sapply(X,function(x){ if(x>0){x^2} else{-x^2}}))
}
ggplot(plot,aes(x,y))+geom_point(aes(colour=z))+scale_colour_gradient(trans="super")
It gives an error,
Error in if (x > 0) { : missing value where TRUE/FALSE needed
I don't understand it. I tried to backtrack the mistake, and my guess is that the error happens when trans_new tries to make breaks.
However, i do not understand how the "breaks" parameter works in trans_new.
Is there a ggplot2/Scales hero out there, that can help me transform my color-scale correctly?
It may be relevant that only some datasets gives errors.
There is a vectorized if, called ifelse. It also seems you are missing an extra minus.
super_trans <- function() {
trans_new('super',
function(x) ifelse(x>0, x^0.5, -(-x)^0.5),
function(x) ifelse(x>0, x^2, -(-x)^2))
}
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!
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")
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)