Finding correlation between two variables [closed] - r

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.

Related

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

Is it possible to create a law of probability 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 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.

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.

Fastest way to find the maximum real root of cubic function in R [closed]

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

equivalence of equations [closed]

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
A reviewer of a paper I submitted to a scientific journal insists that my function
f1[b_, c_, t_] := 1 - E^((c - t)/b)/2
is "mathematically equivalent" to the function
f2[b0_, b1_, t_] := 1 - b0 E^(-b1 t)
He insists
While the models might appear(superficially) to be different, the f1
model is merely a re-parameterisation of the f2 model, and this can be
seen easily using highschool mathematics.
I survived High School, but I don't see the equivalence, and FullSimplify does not yield the same results. Perhaps I am misunderstanding FullSimplify. Is there a way to authoritatively refute or confirm the assertion of the reviewer?
If c and b are constant, you can factor them out relatively easily given the property of the power operator:
e^(A + B) = e^A x e^B...
so
e^((c - t)/b) = e^(c/b - t/b) = e^(c/b) x e^(-t/b) = b0 x e^(-t/b)
The latter expression is commonly used to simplify linear differential equation.

Resources