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 3 years ago.
Improve this question
I have a vector of probability and I want to sample one component from this vector by taking into account the value of its probability.
Here is an example:
x = [1/2,1/4,1/4]
The probability of taking 1 in x is 1/2, the probability for 2 and 3 is 1/4. How can I do that in R?
You can use sample function
sample(1:3, 1, prob = c(0.5, 0.25, 0.25))
It selects one value from c(1, 2, 3) with the probability of c(0.5, 0.25, 0.25) respectively.
Read ?sample for more details on how it works.
Related
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)
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 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
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 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
How do I know that the following variables are related (in general, any two variables)?
Below, there is an obvious relation between x and y however, the 'cor' function is giving me '0'. Is there any function in R that can detect both linear and non-linear relation?
> x <- c(-2, -1, 0, 1, 2)
> y <- c(4, 1, 0, 1, 4)
>
> cor(x,y)
[1] 0
>
Edit:
I am thinking to go for MIC/MINE algorithm despite many criticisms of this algorithm by top statisticians.
Have a look at MINE (1). They also provide a wrapper for R.
(1) Reshef, D.N, Y.A Reshef, H.K Finucane, S.R Grossman, G. McVean, P.J Turnbaugh, E.S Lander, M. Mitzenmacher, and P.C Sabeti. “Detecting Novel Associations in Large Data Sets.” Science 334, no. 6062 (2011): 1518–1524.