F Distribution in R [closed] - r

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))

Related

how to find expected value of 1000 random number of Poisson Distribution [closed]

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 2 years ago.
Improve this question
Generate a vector of 1000 Poisson random numbers with λ = 3. Make a histogram and a boxplot of the 1000 numbers. Find the expected value of the vector in Rstudio
Try with this:
set.seed(123)
#Code
v <- rpois(1000,lambda = 3)
#Hist
hist(v)
#Boxplot
boxplot(v)
#Mean
mean(v)

Setting limits to a rnorm function in RStudio [closed]

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).

maximizing with two functions in R [closed]

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"])

How to generate random values from a normal distribution with specific mean in R [closed]

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 5 years ago.
Improve this question
I have a book with this equation, but I am not sure how to translate this to code in R. I was wondering if someone could provide an example.
I want to generate random values from this distribution.
You can use rnorm to generate random values:
set.seed(100)
mu <- 5 # or whatever your mean is
n <- 10 # the number of random values you wish to generate.
x <- rnorm(n, mean = mu) #the function's default standard deviation is already 1
x
[1] 4.497808 5.131531 4.921083 5.886785 5.116971 5.318630 4.418209 5.714533
[9] 4.174741 4.640138

How to solve for unknown variables in r? [closed]

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.

Resources