writing a loop and the integrate function - r

I would like to calculate integral from two columns in my data frame
the integral is based on a function; 1/x, I quess I need to write a loop . Can you help with writing a loop and the integrate function?
This is the sample data frame;
upper_concentration<-c(1:200, 1)
lower_concentration<-upper_concentration*0.9
df = data.frame(upper_concentration,lower_concentration)
for (i in 1:(length(df))){
integral <- function (x) {1/df$upper_concentration}
result <- integrate(integral, lower = df$upper_concentration, upper =upper_concentration*0.9)
}

The built-in integrate function will perform the integral for the full range.
lower <- 1
upper <- 200
integral <- function (x) {1/x}
result <- integrate(integral, lower, upper)
Now, if you want the integral for each portion of the dataframe then a loop is necessary.
result<-vector(list, length=nrow(df)) #initialize an empty vector
integral <- function (x) {1/x}
for (i in 1:(nrow(df))){
result[[i]] <- integrate(integral, df$lower_concentration[i], df$upper_concentration[i])
}

Related

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.

sampling random values each iteration

I have some simulated data, on top of the data I add some noise to see how the noise affects my data for further analyses. I created the following function
create.noise <- function(n, amount_needed, mean, sd){
set.seed(25)
values <- rnorm(n, mean, sd)
returned.values <- sample(values, size=amount_needed)
}
I call this function in the following loop:
dataframe.noises <- as.data.frame(noises) #i create here a dataframe dim 1x45 containing zeros
for(i in 1:100){
noises <- as.matrix(create.noise(100,45,0,1))
dataframe.noises[,i] <- noises
data_w_noise <- df.data_responses+noises
Estimators <- solve(transposed_schema %*% df.data_schema) %*% (transposed_schema %*% data_w_noise)
df.calculated_estimators[,i] <-Estimators
}
The code above always returns the same values, one solution I tried is sending i as parameter(which i think isn't correct) for each iteration I add i to the set.seed(25+i)
This gives me a unique value for each iteration, butas mentioned I don't think that this is the correct way to go with it.

Vectorize a two argument function

I have a covariance function type of two lags: h1 and h2. I am trying to avoid for loops to create the covariance function matrix.
When I type cov1 it does not give me a matrix. Just a vector if I type for example covmatrix(h1=1:5,h2=1:5). How can I obtain for example the whole 5 by 5 matrix.
I tried all apply functions, and the new vectorize function (with lower case v)
R code:
x=arima.sim(n = 100 , list(ar = .5))
cov=function(h1,h2){
(1/n)*sum((x[1:(n-h1-h2)]-mean(x))*(x[(1+h1):(n-h2)]-mean(x))*(x[(1+h1+h2):n]-mean(x)))
}
covmatrix=Vectorize(cov)
A simple double-apply should get you what you are looking for. Note how the return value of the vectorized function is equal to the diagonal of the covmatrix.
test <- sapply(1:5, function(x) sapply(1:5, function(y) cov(x, y)))
all.equal(diag(test), covmatrix(1:5, 1:5))

Returning 'traditional' notations of functions in the context of fourier interpolation

in numerical analysis we students are obligated to implement code in R that given a function f(x) finds its Fourier interpolation tN(x) and computes the interpolation error
$||f(x)-t_{N}(x)||=\int_{0}^{2\pi}$ $|f(x)-t_{N}(x)|^2$
or a variety of different $N$
I first tried to compute the d-coefficients according to this formular:
$d = \frac 1N M y$
with M denoting the DFT matrix and y denoting a series of equidistant function values with
$y_j = f(x_j)$ and
$x_j = e^{\frac{2*pi*i}N*j}$
for $j = 1,..,N-1$.
My goal was to come up with a sum that can be described by:
$t_{N}(x) = \Sigma_{k=0}^{N-1} d_k * e^{i*k*x}$
Which would be easier to later integrate in sort of a subsequently additive notation.
f <- function(x) 3/(6+4*cos(x)) #first function to compare with
g <- function(x) sin(32*x) #second one
xj <- function(x,n) 2*pi*x/n
M <- function(n){
w = exp(-2*pi*1i/n)
m = outer(0:(n-1),0:(n-1))
return(w^m)
}
y <- function(n){
f(xj(0:(n-1),n))
}
transformFunction <- function(n, f){
d = 1/n * t(M(n)) %*% f(xj(0:(n-1),n))
script <- paste(d[1])
for(i in 2:n)
script <- paste0(script,paste0("+",d[i],"*exp(1i*x*",i,")"))
#trans <- sum(d[1:n] * exp(1i*x*(0:(n-1))))
return(script)
}
The main purpose of the transform function was, initially, to return a function - or rather: a mathematical expression - which could then be used in order to declarate my Fourier Interpolation Function. Problem is, based on my fairly limited knowledge, that I cannot integrate functions that still have sums nested in them (which is why I commented the corresponding line in the code).
Out of absolute desperation I then tried to paste each of the summands in form of text subsequently, only to parse them again as an expression.
So the main question that remains is: how do I return mathmatical expressions in a manner that allow me to use them as a function and later on integrate them?
I am sincerely sorry for any misunderstanding or confusion, as well as my seemingly amateurish coding.
Thanks in advance!
A function in R can return any class, so specifically also objects of class function. Hence, you can make trans a function of x and return that.
Since the integrate function requires a vectorized function, we use Vectorize before outputting.
transformFunction <- function(n, f){
d = 1/n * t(M(n)) %*% f(xj(0:(n-1),n))
## Output function
trans <- function(x) sum(d[1:n] * exp(1i*x*(0:(n-1))))
## Vectorize output for the integrate function
Vectorize(trans)
}
To integrate, now simply make a new variable with the output of transformFunction:
myint <- transformFunction(n = 10,f = f)
Test: (integrate can only handle real-valued functions)
integrate(function(x) Re(myint(x)),0,2)$value +
1i*integrate(function(x) Im(myint(x)),0,2)$value
# [1] 1.091337-0.271636i

Integration of a vector return one value

I am using R to do some multivariate analysis. For this work I need to integrate the trivariate PDF.Since I want to use this in a MLE, a want a vector of integration. Is there a way to make Integratebring a vector instead of one value.
Here is simple example:
f1=function(x, y, z) {dmvnorm(x=as.matrix(cbind(x,y,z)), mean=c(0,0,0), sigma=sigma)}
f1(x=c(1,1,1), y=c(1,1,1), z=c(1,1,1))
integrate(Vectorize(function(x) {f1(x=c(1,1,1), y=c(1,1,1), z=c(1,1,1))}), lower = - Inf, upper = -1)$value
Error in integrate(Vectorize(function(x) { : evaluation of function gave a result of wrong length
To integrate a function of one variable, with vector values,
you can transform the function into n functions with real values,
and integrate each of them.
This is very inefficient (when integrating the i-th function,
I evaluate all the functions, and discard all but one value).
# Function to integrate
d <- rnorm(10)
f <- function(x) dnorm(d, mean=x)
# Integrate those n functions separately.
n <- length(f(1))
r <- sapply( 1:n,
function(i) integrate(
Vectorize(function(x) f(x)[i]),
lower=-Inf, upper=0
)$value
)
r
For 2-dimensional integrals, you can check pracma::integral2,
but the same manipulation (transforming a bivariate function with vector values
into n bivariate functions with real values) will probably be needed.

Resources