optim function with a vector of parameters in R - r

I need to find a beta (3-element vector) which minimizes QRsum, defined as below:
#indicator function
I=function(x,min,max){
if (min<=x && x<max){i=1} else {i=0}}
#QR check function
rho=function(a, theta){
return(abs(theta-I(a,-Inf,0))*abs(a))
}
#QR sum
QRsum=function(beta,y,X,theta){
sum=0
for (i in 1:length(y)){
sum=sum+rho(y[i]-t(X[i,])%*%beta,theta)
}
sum=1/length(y)*sum
return(sum)
}
result=optim(initial_beta,QRsum(beta,y,X,0.05),method="BFGS")
Running optim function finished with following message:
Browse[1]> Q
> result=optim(initial_beta[1,],fn=QRsum(beta[1,],y,X,0.05),method="BFGS")
Error in (function (par) : could not find function "fn"
Arguments of QRsum are y-a vector, X - a matrix with 3 columns and beta - a vector of 3 elements which should be optimized. QRsum function works fine when calling, but here it somehow fails. Is y and X are the objects I defined earlier or they are unknown also despite I defined them before? What should I put as first argument of my function - arbitrary value of a vector?
I must say that I use optim for the very first time, so I suppose I'm missing something here, but I can't figure out what exactly based on help.

You need to modify the call of the optim package to the following:
# define some data / value with which you want to use in the optimization
y_val <- ...
X_val <- ...
theta_val <- 0.05
# call optim / optimize beta with given values for y, X and theta
optim(par=initial_beta, fn=QRsum, y=y_val, X=X_val, theta=theta_val, method="BFGS")

Related

R integrate function error (simple syntax error I can't find) (Error in integrate(f, a, b) : length(lower) == 1 is not TRUE)

I have a function whose value is the integral of another well-established function. Then I want to create a new function whose value is the integral of the function, whose value is the integral :) The first step is OK, but then the second gives a error. Here's the code:
A <- 1
f <- function(a) a^2
g <- function(a,b) integrate(f,a,b)$value
h <- function(s) g(s,A)
integrate(h,0,A)
The error I get is:
Error in integrate(f, a, b) : length(lower) == 1 is not TRUE
What am I doing wrong? Thanks a lot!
I tried to Google and did not find this specific situation.
I also tried to actually integrate g to obtain h, numerically ad hoc: take dx>0, then dx*sum(sapply(seq(a,b,by=dx),h)) just to obtain a number, but I would like to do it with an integrated function.
The problem is that integrate will not accept an array of values for the lower bound, only a single number. That means your function h(s) cannot accept a vector of values. But integrate also requires that the function to be integrated is vectorized. You can fix this with the function Vectorize.
If you change the definition of h, your code will work.
h <- Vectorize(function(s) g(s,A))
integrate(h,0,A)
0.25 with absolute error < 2.8e-15
Look at the ?integrate
integrate(f, lower, upper)
f should be a function taking a numeric and returning a numeric
your f <- function(a) a^2 is ok.
the next 2 arguments should be the limits of integration ("numbers")
integrate(f,2,5)
39 with absolute error < 4.3e-13
(1/3) * (5^3-2^3)
39
the error message length(lower) == 1 is not TRUE
means that the second argument is not "one" number

Bootstrap function for dataframe - passing a function as an argument in R

I am trying to create a bootstrap function for my assignment. The requirement is as follows:
Compute the bootstrap standard error for: - mean() and -
median() and - the top quartile and - max() and - the
standard deviation of the price. One way to approach this is to define
a new function for each. Another is to write a bootstrap_func
function that takes an additional argument called fun, and then you
call it bootstrap_func(B, v, median) to have the same effect as
bootstrap_median. Implement this function bootstrap_func.
Example call to this function: bootstrap_func(1000, vienna_data$price, mean). Generalize the function further so that the
second argument ($v$) can be a vector or a dataframe. Therefore, the
third argument can be a function that takes a vector -- such as mean
-- or a function that takes a dataframe and returns some number -- such as a function that computes a linear model and returns the
estimate of the linear model. Use this new function to compute
bootstrap estimators for the standard errors of some linear model
coefficients on the vienna dataset -- e.g. the effect of stars on
prices. You have to define and name a function that returns the
coefficient of the right linear model (say estimate_of_stars_on_prices <- ...), and pass this function as one
of the arguments to bootstrap_func.
I created the bootstrap function for the vector like this
sim <- function(v) {
sample(v, replace = TRUE)
}
bootstrap_func <- function(B, v, fun) {
sd(replicate(B, fun(sim(v))))
}
quartile <- function(x) {quantile(x, 0.75)}
So I can call an example like this
bootstrap_func(100, hotels_vienna$price, mean)
bootstrap_func(100, hotels_vienna$price, quartile)
And I think it works fine enough. But I have trouble generalizing it to take also the dataframe and the function that gets the coefficient. My function to get the coefficient is
coef <- function(v, y, x) {
Y <- v[,y]
X <- v[,x]
lmm <- lm(Y ~ X, v)
lmm$coefficients[[2]]
}
coef(hotels_vienna, 2, 12) # this works, col2 = price, col12= distance, result = -22.78177
This is my attempt at the generalized code
df_bootstrap_func <- function(B, v, fun, ...) {
new_v <- function(v) {sample(v, replace = TRUE)}
sd(replicate(B, fun(new_v)))
}
df_bootstrap_func(100, hotels_vienna, coef)
# does not work, throw Error in v[, y] : object of type 'closure' is not subsettable
I have tried multiple versions of the df_bootstrap_func but no success, so I think I need a new approach to the coefficient function. I appreciate any input. TIA.

Problems with Gaussian Quadrature in R

I'm using the the gaussquad package to evaluate some integrals numerically.
I thought the ghermite.h.quadrature command worked by evaluating a function f(x) at points x1, ..., xn and then constructing the sum w1*f(x1) + ... + wn*f(xn), where x1, ..., xn and w1, ..., wn are nodes and weights supplied by the user.
Thus I thought the commands
ghermite.h.quadrature(f,rule)
sum(sapply(rule$x,f)*rule$w)
would yield the same output for any function f, where ''rule'' is a dataframe which stores the nodes in a column labelled ''x'' and the weights in a column labelled "w". For many functions the output is indeed the same, but for some functions I get very different results. Can someone please help me understand this discrepancy?
Thanks!
Code:
n.quad = 50
rule = hermite.h.quadrature.rules(n.quad)[[n.quad]]
f <- function(z){
f1 <- function(x,y) pnorm(x+y)
f2 <- function(y) ghermite.h.quadrature(f1,rule,y = y)
g <- function(x,y) x/(1+y) / f2(y)*pnorm(x+y)
h <- function(y) ghermite.h.quadrature(g,rule,y=y)
h(z)
}
ghermite.h.quadrature(f,rule)
sum(sapply(rule$x,f)*rule$w)
Ok, that problem got me interested.
I've looked into gaussquad sources, and clearly author is not running sapply internally, because all integrands/function shall return vector on vector argument.
It is clearly stated in documentation:
functn an R function which should take a numeric argument x and possibly some parameters.
The function returns a numerical vector value for the given argument x
In case where you're using some internal functions, they're written that way, so everything works.
You have to rewrite your function to work with vector argument and return back a vector
UPDATE
Vectorize() works for me to rectify the problem, as well as simple wrapper with sapply
vf <- function(z) {
sapply(z, f)
}
After either of those changes, results are identical: 0.2029512

Z-transform of a function in R language

I have a function f(x) that gives me results in time domain. I want to get the z-transform of that function so that I can compare both. I know this would be easy to calculate in MATLAB. However, I'm wondering if there is a way to do it in R by a package or writing a code from scratch. The reason for using R because I have done most of the required work and other calculations in R.(Plus R is free)
I searched and found some suggestions to use scale. However, I think it has to do with data not the function. Also, I found a package GeneNet which has a function called z-transform. However, it gives a vector of numbers. I want to get the z-transform as function of z.
By definition z-transform calculated from :
Update for simplicity:
if we have f(x)= x, where x= 0,1,2,3,4,....100. I want to get the z-transform for the given function f(x).
Based on the above definition of z-transform and by substitution:
x(z) = SUM from n=0 to n=100 of (Xn) *(Z ^-n)
for n=0 => x(z)= (0) (Z^-0)
for n=1 => x(z)= 0 + (1) (z^-1)
for n=2 => x(z)= 0 + (1) (z^-1) + (2) (z^-2)
...
..
Any suggestions?
Seems like you've got two problems: calculating f(x) = x XOR 16, and then computing the z-transform of the result.
Here's an (updated) z-transform function which will work on a defined x optionally an arbitrary n vector (with the default assumption that n starts at 0 and goes up by one for each value of x). It now returns a function that can be used to evaluate various z values:
ztransform = function(x, n = seq_along(x) - 1) {
function(z) sum(x * z ^ -n)
}
my_z_trans = ztransform(x = 0:100, n = 0:100)
my_z_trans(z = 1)
# [1] 5050
my_z_trans(z = 2)
# [1] 2
my_z_trans(z = 3)
# [1] 0.75

computing an intergral with multiple variables in R

Hi I have a equation like the following that I want to calculate.
The equation is given by :
In this equation x is an arrary from 0 to 500.
The value of t = 500 i.e upper limit of the integration.
Now I want to compute c as c(500,x).
The code that I have written so far is as follows:
x <- seq(from=0,by=0.5,length=1000)
t=500
integrand <- function(t)t^(-0.5)*exp((-x^2/t)-t)
integrated <- integrate(integrand, lower=0, upper=t)
final <- pi^(-0.5)*exp(2*x)*integrated
The error I get is as follows:
Error in integrate(integrand, lower = 0, upper = t) :
evaluation of function gave a result of wrong length
In addition: Warning messages:
1: In -x^2/t :
longer object length is not a multiple of shorter object length
2: In -x^2/t - t :
longer object length is not a multiple of shorter object length
3: In t^(-0.5) * exp(-x^2/t - t) :
longer object length is not a multiple of shorter object length
But it doesn't work because there is a variable x inside the integrand which is an arrary. Can anyone suggest how can I compute the integration first and then calculate the total expression for each value of x ? If I change the value of x in the integrand to constant I can compute integration but I want to compute for all the values of x from 0 to 500.
Thank you so much.
Well, here is some code, but it blows up after t=353:
Cfun <- function(XX, upper){
integrand <- function(x)x^(-0.5)*exp((-XX^2/x)-x)
integrated <- integrate(integrand, lower=0, upper=upper)$value
(final <- pi^(-0.5)*exp(2*XX)*integrated) }
sapply(1:400, Cfun, upper=500)
I'd put the loop over values for x outside the integration. Iterate over the x-values and perform the integration for each one inside. Then you'll have C(x) as a function of x suitable for plotting.
You realize, of course, that the indefinite integral can be evaluated:
http://www.wolframalpha.com/input/?i=integrate+exp%28-%28c%2Bt%5E2%29%2Ft%29%2Fsqrt%28t%29
Maybe that will help you see what the answer looks like before you get started.

Resources