Reputation: 59435
Why does plot(Vectorize(f))
, where f
is a function, work, but curve(Vectorize(f))
does not? This inconsistency bothers me a bit. Here is a full reproducable example:
set.seed(123)
N <- 9
X <- rnorm(N)
a <- 0.2
b <- 0.8
sigma <- 0.7
eps <- rnorm(N, 0, sigma)
Y <- a + b * X + eps
cor_p_value <- function(new_Y1)
{
Y[1] <- new_Y1
cor.test(X,Y)$p.value
}
lm_F_p_value <- function(new_Y1)
{
Y[1] <- new_Y1
anova(lm(Y ~ X))$Pr[1]
}
plot(Vectorize(cor_p_value), xlim = c(-50,50), lwd = 3)
curve(Vectorize(lm_F_p_value), col = "red", lwd = 3, lty = 2, add = TRUE)
# Error in curve(Vectorize(lm_F_p_value), col = "red", lwd = 3, lty = 2, :
# 'expr' must be a function, or a call or an expression containing 'x'
So for curve
, I must do this annoying workaround:
cu <- Vectorize(lm_F_p_value)
curve(cu, col = "red", lwd = 3, lty = 2, add = TRUE)
But why? Vectorize
returns the same function, plot
takes it, curve
does not. All these functions are from the base built-in R packages, so I expect them to cooperate and be consistent. Probably a bug?
Upvotes: 2
Views: 67
Reputation: 886938
According to ?curve
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.
curve(Vectorize(lm_F_p_value)(x), col = "red", lwd = 3, lty = 2,
add = TRUE, ylab = "y")
-output
Upvotes: 3