lchoose function in R - r

My understanding of lchoose function in R is simply lchoose(a,b) = log(choose(a,b)).
However, I found that:
temp <- 7.9999993
k <- 8
choose(temp,k)
[1] 0
lchoose(temp,k)
[1] 0
log(choose(temp,k))
[1] -Inf
So lchoose is not log of the choose function output.
Why is this happening?

In the discrete case (i.e discrete n), choose(n,k) computes the number of distinct k-element subsets from a set of n elements, so if k > n, then you are counting subsets of a set which have more elements that the corresponding set. Since there are no such subsets, then the answer is zero.
In general, for an n which is a real number, the function can still be computed, but however, the function still has to have the same meaning over discrete values, so for k>n the function has a value of zero. If you look at the definition of the binomial function with real n (see here) you'll see that the answer will be zero, but I tried to explain it, hopefully, in an intuitive manner.

Related

optim() when optimizing for one parameter in R

I'm trying to find the single value of scale in function bayesmeta::qhalfnormal such that the first and the second elements of the vector low_high <- c(.1, 1) have .025 and .975 probability of happening in it, respectively.
In other words, for what value of scale .1 can have .025 and 1 can have .975 probability.
So, I have one parameter (scale) to optimize, and expect a single value for it. I'm using optim below but this way, I get two values for scale.
Is there a better optimization function to give me a single value for scale?
library(bayesmeta)
low_high <- c(.1, 1)
alpha <- c(.025, .975)
f <- function(x) {
low_high - qhalfnormal(alpha, scale = x) }
optim(low_high, function(x)sum(f(x)^2))
# $par
> [1] 3.1939758 0.4461607 # I expect a single value for `scale`
# But it seems `optim()` has acted like `Vectorize(optimize)` looping over
# elements of `low_high` vector.
#anonymous.asker is correct that passing a vector of length 2 is confusing optim(). What is happening is that qhalfnormal() is vectorizing both over the quantiles and the vector of scale values you gave it: e.g. qhalfnormal(c(0.1, 1), c(0.025, 0.975)) returns a two-element vector comprising (1) the 0.025 quantile for a scale parameter of 0.1 and (2) the 0.975 quantile for a scale parameter of 1. These then get collapsed to a single output value by the sum-of-squares operation ... (what you wanted, I think, was to evaluate qhalfnormal() for both quantile levels for a single scale parameter).
If you specify a single value that is close enough to the true value you get an answer, and a warning suggesting that you not use Nelder-Mead:
optim(0.45, function(x)sum(f(x)^2))
If your starting value is too far from the solution you get a warning and an error as soon as the algorithm tries a negative value for the parameter ("scale > 0 is not TRUE").
The sensible way to do this is to specify method="Brent" (as suggested by the warning, at which point you also need to specify bounds:
optim(1, function(x)sum(f(x)^2), method="Brent", lower=0, upper=10)
This returns 0.4461; this is indeed the argmin (parameter corresponding to the minimum value) for this problem. As #Onyambu points out in comments though, it doesn't really solve the larger problem (which is to try to reduce both values to 0); it solves the problem as posed, which is to minimize the objective function ...
You are passing a vector of length 2 as the initial estimate. If you want to set bounds for your variable, that's under different arguments in optim.

Does R have a function for the logarithm that deals with negative numeric values?

In mathematics, the exponential and logarithm functions can be generalised from the real numbers to the complex numbers. The exponential function is generalised using Euler's formula and the logarithm is generalised to the complex logarithm. The latter allows inputs that are complex numbers or negative real numbers.
Thankfully, the exponential and logarithmic function in R accommodate complex inputs. Both functions can take complex inputs and produce the appropriate exponential or logarithm of that input. However, if you put in a negative number as a numeric value (as opposed to a complex value), the log function does not produce the proper (complex) output for this input. Here is an example of what happens.
#Define negative real value as numeric/complex object
minusfour.numeric <- -4
minusfour.complex <- complex(real = -4, imaginary = 0)
#Apply the log function to these inputs
log(minusfour.complex)
[1] 1.386294+3.141593i
log(minusfour.numeric)
[1] NaN
Warning message:
In log(minusfour.numeric) : NaNs produced
Ideally, it would be nice if the log function gave the proper (complex) output when you give a negative numeric value as the input. unfortunately it does not appear to be programmed to do this.
My Question: Is there another logarithm function programmed in R that accommodates negative numeric inputs (i.e., gives the appropriate complex output for these inputs)?
Based on the upvoted comment from Hugh it looks like the simplest method is just to write the function as a variation of the underlying base log function. I've decided to go with a version that converts back to a real number at the end of the computation if none of the outputs have imaginary parts. Here is the code:
Log <- function(x, base = exp(1)) {
LOG <- base::log(as.complex(x), base = base)
if (all(Im(LOG) == 0)) { LOG <- Re(LOG) }
LOG }
This code works for positive or negative numeric values and gives a numeric output when you give it a non-negative numeric input.
#Apply the Log function to +4 and -4
Log(-4)
[1] 1.386294+3.141593i
Log(4)
[1] 1.386294

R Repeat function from first subvector of vector until total vector length reached

I have a vector epsilon of length N. I am applying the function bw.CDF.pi(x, pilot="UCV") from the sROC package to compute bandwidths for cdf Kernel estimation.
My goal is to repeat this bandwidth function for every subvector from epsilon from the beginning value on. Stated otherwise, I would like to apply this function for the first value in epsilon, then for the first two values in epsilon, then for the first three values in epsilon, continiuing until the function is applied fot the total vector epsilon. Finally i want to have then N values for the bandwidth.
How can I accomplish this?
Apparently you need a vector of 2 elements for the function bw.CDF.pi to run. If you want to run it for the first 2 elemts of a vector, then the first 3, etc, you can do the following. Note that the data example is the one in the help page for the function.
library(sROC)
set.seed(100)
n <- 200
x <- c(rnorm(n/2, mean=-2, sd=1), rnorm(n/2, mean=3, sd=0.8))
lapply(seq_along(x)[-1], function(m) bw.CDF.pi(x[seq_len(m)], pilot="UCV"))

How do I get started with this?

So I am stuck on this problem for a long time.
I was think I should first create the two functions, like this:
n = runif(10000)
int sum = 0
estimator1_fun = function(n){
for(i in 1:10000){
sum = sum + ((n/i)*runif(1))
)
return (sum)
}
and do the same for the other function, and use the mse formula? Am I even approaching this correctly? I tried formatting it, but found that using an image would be better.
Assuming U(0,Theta_0) is the uniform distribution from 0 to Theta_0, and that Theta_0 is a fixed constant, I would proceed as follows:
1. Define Theta_0. Give it a fixed value.
2. Write the function that gives a random number from that distribution
- The distribution function is runif(0,Theta_0).
- Arguments could be Theta_0 and N.
3. Sample it a few thousand (or whatever) times into a vector X.
4. Calculate the two estimates.
5. Repeat steps 3 & 4 for more samples
6. Plot the two estimates against the number of samples and
see if it is approaching Theta_0

confused in understanding an rbinom output

p1 <- c(.25,.025,.025,.1,.2,.4)
T <- sample(1:6,size=N,replace=TRUE, prob=someprobabilityvector)
Y <- rbinom(N,1,p1[c(T)])
HI folks, I am new to R and programming in general and need some help with understanding sth basic. could someone explain to me one what is happening in vector Y above. I figure out what p1[c(T)] does above. But have no idea what vector Y is doing. All help is appreciated in advance.
The first line of your code creates a vector of six probabilities:
p1 <- c(.25,.025,.025,.1,.2,.4)
In the second line, you randomly choose N values from the numbers one to six (with replacement). The probability for each value is specified in someprobabilityvector. Hence, the function will return a vector of length N including values between 1 and 6
T <- sample(1:6,size=N,replace=TRUE, prob=someprobabilityvector)
In the third line, N random numbers from a binomial distribution with one trial and probablities specified in p1[c(T)] are generated. c(T) is the same as T: the vector including values from 1 to 6. The vector is used for indexing the vector p1. Hence, p1[c(T)] will return a vector including N values from vector p1.
Y <- rbinom(N,1,p1[c(T)])
Since the specified binomial distribution has one trial only, the vector Y will contain zeroes and ones.

Resources