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.
Related
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
Good evening,
Even though I know it will "destroy" an actual normal distribution, I need to set a maximum and a minimum to a rnorm function in R.
I'm using survival rates in vultures to calculate population trends and although I need it to fluctuate, for logic reasons, survival rates can't be over 1 or under 0.
I tried doing it with if's and else's but I think there should be a better way to do it.
Thanks!
You could sample from a large normalized rnorm draw:
rbell <- function(n) {
r <- rnorm(n * 1000)
sample((r - min(r)) / diff(range(r)), n)
}
For example:
rbell(10)
#> [1] 0.5177806 0.5713479 0.5330545 0.5987649 0.3312775 0.5508946 0.3654235 0.3897417
#> [9] 0.1925600 0.6043243
hist(rbell(1000))
This will always be curtailed to the interval (0, 1).
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)
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 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 7 years ago.
Improve this question
I can't figure out how to plot the F distribution in R, given two degrees of freedom using standard normal variates. Any suggestions?
You can use curve()
curve(df(x, df1=1, df2=2), from=0, to=5)
Here is the documentation of curve()
df is the density of the F distribution. This can be found in ?distributions and follows the standard naming conventions dnorm for normal distribution, dt for t distribution, etc. The F distribution has two degrees of freedom parameters. Use pf if you want the CDF.
x = seq(0, 5, length = 100)
plot(x, df(x = x, df1 = 1, df2 = 1))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Whats is the fastest way to find the maximum root of a cubic function in R?
a x^3 + b x^2 + c x + d = 0
Is there anything wrong with the base function polyroot?
Description
Find zeros of a real or complex polynomial.
An example of a cubic
polyroot(c(1,3,3,1))
# [1] -1+0i -1+0i -1-0i
Here is a function to find the maximum non-complex root of a polynomial...
maxReal <- function(params){
x <- polyroot(params)
reals <- sapply(x, function(i) isTRUE(all.equal(Im(i),0)))
max(Re(x)[reals])
}