2

what is the easiest way to create random points on a circle of specific radius with noise in R? is there a library that does this kind of things?

enter image description here

1
  • How should the points be distributed ? Commented Feb 15, 2022 at 21:36

3 Answers 3

5
theta = runif(100, 0,2*pi) x = cos(theta) + rnorm(100, 0, 0.03) y = sin(theta) + rnorm(100, 0, 0.03) plot(x,y, pch=20) 

Noisy circle

Sign up to request clarification or add additional context in comments.

3 Comments

It seems strange to me that you're drawing the radial noise independently for the x and y components rather than a single r = rnorm(...). OP didn't specify any particular distribution, but this might introduce something a little weirder than the other way (or at least be a bit harder to make a definitive statement for what the variance of the radius is).
I see this as two separate random factors - position on the circle and variation from the circle. I don't see how to view it as one dimensional, although if you can describe it as such, that would be interesting.
@GregorThomas I agree, though I'm guessing you mean r = rlnorm(...). It is strange to introduce three sources of variation (one runif and two rnorm) in a space with two dimensions.
4

It would be more natural to sample:

  • the polar angle theta from a uniform distribution supported on [0, 2*pi),
  • the polar radius r from a lognormal distribution supported on (0, Inf), so that log(r) is normally distributed with mean meanlog and standard deviation sdlog.

So you could try something like this:

set.seed(1L) n <- 100L theta <- runif(n, 0, 2 * pi) r <- rlnorm(n, meanlog = log(2), sdlog = 0.1) plot(r * cos(theta), r * sin(theta), asp = 1) 

enter image description here

And you could generalize to multiple circles like so:

circles <- function(n, mu, sigma) { lr <- Map(rlnorm, n = n, meanlog = mu, sdlog = sigma) N <- length(lr) n <- lengths(lr, FALSE) data.frame(group = rep.int(gl(N, 1L), n), r = unlist(lr, FALSE, FALSE), theta = runif(sum(n), 0, 2 * pi)) } set.seed(1L) d <- circles(n = c(25L, 100L, 400L), mu = log(c(1, 2, 4)), sigma = c(0, 0.05, 0.1)) str(d) ## 'data.frame': 525 obs. of 3 variables: ## $ group: Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ... ## $ r : num 1 1 1 1 1 1 1 1 1 1 ... ## $ theta: num 3.335 4.303 2.408 6 0.744 ... par(mfrow = c(1L, 2L)) with(d, { plot(r * cos(theta), r * sin(theta), asp = 1, col = group) plot(r * cos(theta), r * sin(theta), asp = 1, col = cut(theta, 0:3 * 2 * pi / 3)) }) 

enter image description here

The function circles is vectorized in n, mu, and sigma, where n is the number of points in a circle and mu and sigma are the mean and standard deviation of the log radius.

Comments

2

We can use 1i to construct polar coordinates (complex format) and then plot it

set.seed(1) n <- 100 plot(rnorm(n, 1, 0.1) * exp(1i * runif(n, 0, 2 * pi))) 

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.