I have a moment generating function, so I differentiate it and substitute 0 for the moment of X. I don't know how to do it in R, can you please enlighten me.
In Octave,
pkg load symbolic; #octave symbolic package which itself uses sympy
syms y, x; #declare x,y as symbols
mgf = y / (y - x); #mgf for an exponential variable
mgf1 = diff(mgf, x, 1); #1st derivative of mgf
mgf1_0 = subs(mgf1, x, 0); #substituting 0 in mgf1 for E(X)
I understand R doesn't have built-in symbolic functionality but given what I am trying to do is simply differentiation and substitution, something frequently needed when dealing with pdf/mgf/functions of RVs and other series expansion, I would like to know how I can perform in R in a simple manner (so I can focus on what to calculate, not how).
mgf <- expression(y / (y-x));
mgf1 <- D(mgf, "x");
#no symbolic substitute for x? eval won't work as y remains a variable
library(rlang)
(dexpr <- D(expression(y/(y-x)),"x"))
#evaluate it at y =1 , x= 0
eval(dexpr,envir = list(y=1,x=0))
# substute 1 for y
(dexpr_y1 <- rlang::parse_expr(stringr::str_replace_all(deparse(dexpr),"y","1")))
#evaluate at x=2 and x=3
eval(dexpr_y1,envir=list("x"=2:3))