Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial sorting of a vector

Tags:

r

Say I have a vector of random numbers, I can order them lowest to highest:

set.seed(1)
x <- runif(20)
v <- x[order(x)]

Now, say I want to order them but with some degree of noise.

I can randomly move elements like this:

z <-sample(1:20,2)
replace(v, z, v[rev(z)])

but this doesn't necessarily move closely related values. I could be equally likely to randomly switch the 1st and 20th values as the 5th and 6th. I would like to have some control over the switching, so I can switch more closely related values.

Ideally, I would be able to reorder the vector to have a specific Spearman's correlation. Say rather than the Spearman correlation of rank order being 1 when they are perfectly ordered, is there a way to reorder that same vector of numbers to have e.g. a Spearman's correlation of 0.5 ?

like image 360
jalapic Avatar asked Aug 12 '19 18:08

jalapic


1 Answers

What if you added some noise to their rankings. This will makes sure values don't get moved too far away from the starting point. For example

set.seed(1)
N <- 50
D <- 3 # controls how far things can move
x <- runif(N)
v <- x[vx <- order(rank(x) + runif(N, -D, D))]
z <- x[order(x)]

layout(matrix(c(1,3,2,3), nrow=2))
plot(v, main ="Ordered")
plot(z, main ="Mixed")
plot(v, z, xlab="ordered", ylab="mixed"); abline(0,1)

enter image description here

like image 51
MrFlick Avatar answered Sep 18 '22 01:09

MrFlick