Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm new to R, and a formula I need to enter includes functions that are beyond the scope of my mathematics experience. In particular, I don't understand what's going on with the subscript to the gamma function. Is this an incomplete gamma function, and if so, is it upper or lower?
Anyhow, the formula is attached in the image. This is the CDF for the 4 parameter Generalized Gamma distribution, taken from a statistical software manual. How would I specify this in R? Any help is much appreciated.
This can be completely wrong, but it seems that function F in the image is a combination of an upper incomplete gamma function and other terms. The subscript seems to be a transformation of x:
y <- ((x - gamma)/beta)^k
If so it could be coded as follows.
f <- function(x, a, beta, gamma, k){
y <- ((x - gamma)/beta)^k
pgamma(y, a, lower = FALSE)*gamma(a)
}
The expression
pgamma(y, a, lower = FALSE)*gamma(a)
is a base R way to code the upper incomplete gamma function. Alternatively, package gsl, function gamma_inc could be used replacing that code line with
gsl::gamma_inc(a, y)
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I would like to estimate the parameters of a nonlinear regression model with LAD regression. In essence the LAD estimator is an M-estimator. As far as I know it is not possible to use the robustbase package to do this. How could I use R to do LAD regression? Could I use a standard package?
You could do this with the built-in optim() function
Make up some data (make sure x is positive, so that a*x^b makes sense - raising negative numbers to fractional powers is problematic):
set.seed(101)
a <- 1; b <- 2
dd <- data.frame(x=rnorm(1000,mean=7))
dd$y <- a*dd$x^b + rnorm(1000,mean=0,sd=0.1)
Define objective function:
objfun <- function(p) {
pred <- p[1]*dd$x^p[2] ## a*x^b
sum(abs(pred-dd$y)) ## least-absolute-deviation criterion
}
Test objective function:
objfun(c(0,0))
objfun(c(-1,-1))
objfun(c(1,2))
Optimize:
o1 <- optim(fn=objfun, par=c(0,0), hessian=TRUE)
You do need to specify starting values, and deal with any numerical/computational issues yourself ...
I'm not sure I know how to compute standard errors: you can use sqrt(diag(solve(o1$hessian))), but I don't know if the standard theory on which this is based still applies ...
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I would like to find a maximum economic stress scenario restricted by a limit of the mahalanobis distance of this scenario. For this, I have to consider two functions in the optimization.
To make it easier, we can work with a simplifying problem: We have a simple linear model: y=a+bx. For this I want to minimize: sum(a+bx-y)^2. But also, I have for example the restriction that: (ab*5)/2<30.
To calculate this problem with the excel solver is not a problem. But, how I get this in r?
You could try to incorporate the constraint into the objective function, like this
# example data whose exact solution lies outside the constraint
x <- runif(100, 1, 10)
y <- 3 + 5*x + rnorm(100, mean=0, sd=.5)
# big but not too big
bigConst <- sum(y^2) * 100
# if the variables lie outside the feasible region, add bigConst
f <- function(par, x, y)
sum((par["a"]+par["b"]*x-y)^2) +
if(par["a"]*par["b"]>12) bigConst else 0
# simulated annealing can deal with non-continous objective functions
sol <- optim(par=c(a=1, b=1), fn=f, method="SANN", x=x, y=y)
# this is how it looks like
plot(x,y)
abline(a=sol$par["a"], b=sol$par["b"])
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have distribution of parameter (natural gas mixture composition) expressed in percents. How to test such data for distribution parameters (it should be gamma, normal or lognormal distribution) and generate random composition based on that parameters in R?
This might be a better question for CrossValidated, but:
it is not generally a good idea to choose from among a range of possible distributions according to goodness of fit. Instead, you should choose according to the qualitative characteristics of your data, something like this:
Frustratingly, this chart doesn't actually have the best choice for your data (composition, continuous, bounded between 0 and 1 [or 0 and 100]), which is a Beta distribution (although there are technical issues if you have values of exactly 0 or 100 in your sample).
In R:
## some arbitrary data
z <- c(2,8,40,45,56,58,70,89)
## fit (beta values must be in (0,1), not (0,100), so divide by 100)
(m <- MASS::fitdistr(z/100,"beta",start=list(shape1=1,shape2=1)))
## sample 1000 new values
z_new <- 100*rbeta(n=1000,shape1=m$estimate["shape1"],
shape2=m$estimate["shape2"])
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How do I set two equations equal to each other in R to solve?
For example:
xlog(x)=8273
Find X?
Use equation in the form: x*log(x)-8273 = 0
You should have some idea of the range in which the answer lies. Then use uniroot function:
f <- function(x) (x*log(x)-8273)
uniroot(f, lower=0.1, upper=100000000)$root
[1] 1170.897
Or a more general form:
f <- function(x,y) (x*log(x)-y)
uniroot(f, y=8273, lower=0.1, upper=100000000)$root
[1] 1170.897
It turns out (with a little help from Wolfram Alpha) that this particular solution is related to the Lambert W function (which Wolfram Alpha calls the "product log" function):
library(emdbook)
exp(lambertW(8273)) ## 1170.897
The Lambert W is available in several other R packages (LambertW, spatstat, pracma, condmixt, VGAM) as well.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am trying to find unknown matrix multiply matrix with knowing matrix
A*c=b
where b is defined vector, A is defined matrix 8x8, c is unknown vector.
I know, I can not divide matrix but what is the solution for this situation ??
This is basically a system of simultaneous linear equations. You can solve it using Gaussian elimination.
As for matrix "division", what you really have in mind is an inverse matrix, i.e. a matrix A-1 such that
AA-1=A-1A=I
where I is the identity matrix. If A is invertible then A*c=b is equivalent to c=A-1b.
The answer by Adam is certainly correct, but you should know that calculating the inverse of the matrix might not be the best solution.
Another to look into is LU decomposition and forward-back substitution. It will be more computationally stable that full Gaussian elimination and calculating the inverse.
You solve the problem in steps like this:
Decompose A = LU; now you'll have LUc = b. L is lower triangular; U is upper triangular.
Let y = Uc; solve Ly = b for y.
Now that you have y, solve for the c vector you want: y = Uc.