Youssef
Youssef

Reputation: 49

Understanding curve function in R

I am new to R and trying to better understand how it works.

I don't understand why with function curve we don't need to write function(x), I mean for example with plot we need to write

 plot(function(x) pnorm(x,0,1),-3.5,3.5,col='BLUE',n=1000)

but if we use curve we must just write

  curve(pnorm(x,0,1),-3.5,3.5,col='BLUE',n=1000)

without function(x) before pnorm, why ?

My second question concerning curve is why

curve(x+0 ,-3.5,3.5,col='BLUE',n=1000) 

works fine but

curve(x,-3.5,3.5,col='BLUE',n=1000)

returns an error

Thank you!

Upvotes: 1

Views: 453

Answers (1)

mickey
mickey

Reputation: 2188

When you do

plot(function(x) pnorm(x,0,1),-3.5,3.5,col='BLUE',n=1000)

you're actually calling plot.function. (Notice the difference of the arguments for plot() under ?plot and ?plot.function.) plot.function expects a function for its first argument, and curve expects an expression:

# expr: The name of a function, or a call or an expression written as
#       a function of ‘x’ which will evaluate to an object of the
#       same length as ‘x’.

For your second question, it would seem that curve recognizes x+0 as an expression, but with just x alone it's looking for the object named x (hence the error). If you assign x to something like x=5, then

curve(x+0 ,-3.5,3.5,col='BLUE',n=1000)

will also return an error, since now for sure x+0 is not an expression. The variable x in curve isn't necessarily special; you can change the variable to be whatever with the xname argument).

Upvotes: 1

Related Questions