Eichhorn
Eichhorn

Reputation: 57

How to convert "language" to "numeric" in "R"?

Good day. I'm trying to check whether or not the function has a minimum at point.

For that I use derivatives and Hessian matrices.

The problem is that derivatives are stored in type "language", while I need them in "numeric".

For example, I use

der_x1 <- D(expression(3 + 2 * x_1 + 3 * x_2 + 2 * (x_1)^2 + 2 * x_1 * x_2 + 6 * (x_2)^2), "x_1")
der_x1x1 <- D(der_x1, "x_1")

And get der_x1x1 = 2 * 2.

typeof(der_x1x1) returns "language".

But to find the determinant of the Hessian matrix I need R to simplify 2 * 2 to 4 and convert it to number.

If I try as.numeric(der_x1x1), I get Error: 'pairlist' object cannot be coerced to type 'double'

Upvotes: 1

Views: 141

Answers (1)

sinalpha
sinalpha

Reputation: 383

try the following code:

 eval(der_x1x1)
 x_1 <- 1
 x_2 <- 1
 eval(der_x1)

Upvotes: 1

Related Questions