@whuber makes excellent point regarding the goal of this endeavour, but here's a idea of how to proceed. The idea is to add each cell a corresponding amount of generated noise.
> my.data <- matrix(1:9, nrow = 3)
> my.data
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> random.stuff <- matrix(runif(prod(dim(my.data)), min = -0.00001, max = 0.0001), nrow = 3)
> random.stuff
[,1] [,2] [,3]
[1,] 8.488258e-05 -4.608706e-06 1.869516e-05
[2,] 2.100283e-05 8.500601e-05 7.376338e-05
[3,] 7.625872e-05 6.188059e-05 4.424394e-05
> random.stuff + my.data
[,1] [,2] [,3]
[1,] 1.000085 3.999995 7.000019
[2,] 2.000021 5.000085 8.000074
[3,] 3.000076 6.000062 9.000044
This function takes in a matrix and adds some random noise.
addNoise <- function(mtx) {
if (!is.matrix(mtx)) mtx <- matrix(mtx, byrow = TRUE, nrow = 1)
random.stuff <- matrix(runif(prod(dim(mtx)), min = -0.00001, max = 0.0001), nrow = dim(mtx)[1])
random.stuff + mtx
}
> new.data <- matrix(1:100, nrow = 10)
> addNoise(mtx = new.data)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1.000002 11.00003 21.00001 31.00001 41.00004 51.00009 61.00004 71.00004 81.00002 90.99999
[2,] 2.000002 11.99999 22.00004 32.00000 42.00006 52.00009 62.00000 72.00008 82.00001 92.00001
[3,] 3.000069 13.00006 23.00003 33.00010 43.00004 53.00005 63.00009 73.00005 83.00008 93.00000
[4,] 4.000088 14.00003 24.00006 34.00009 44.00002 54.00002 64.00007 74.00003 84.00007 94.00010
[5,] 5.000073 15.00007 25.00002 35.00009 45.00001 55.00004 65.00002 75.00010 85.00006 95.00000
[6,] 6.000062 16.00001 26.00002 36.00002 46.00008 56.00007 66.00002 76.00000 86.00006 96.00009
[7,] 7.000007 17.00003 27.00005 37.00007 47.00002 57.00000 67.00006 77.00000 87.00007 97.00000
[8,] 8.000019 18.00009 27.99999 37.99999 48.00003 58.00009 68.00001 77.99999 88.00003 98.00001
[9,] 8.999995 19.00010 29.00007 39.00004 49.00008 59.00004 69.00008 79.00005 89.00001 99.00002
[10,] 10.000015 20.00007 30.00003 40.00009 50.00004 60.00009 70.00002 80.00002 90.00003 99.99999
Here's an example of working on vectors. Output will be a 1 by x matrix.
> new.data <- matrix(1:10, nrow = 1)
> addNoise(mtx = new.data)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1.000091 2.000014 2.999991 4.000047 5.000056 6.000042 6.999998 8.000097 9.000066 10.00008
Added some code to show how return()
seems to slow down code.
library(rbenchmark)
addNoise <- function(mtx) {
if (!is.matrix(mtx)) mtx <- matrix(mtx, byrow = TRUE, nrow = 1)
random.stuff <- matrix(runif(prod(dim(mtx)), min = -0.00001, max = 0.0001), nrow = dim(mtx)[1])
out <- random.stuff + mtx
out
}
addNoiseReturn <- function(mtx) {
if (!is.matrix(mtx)) mtx <- matrix(mtx, byrow = TRUE, nrow = 1)
random.stuff <- matrix(runif(prod(dim(mtx)), min = -0.00001, max = 0.0001), nrow = dim(mtx)[1])
out <- random.stuff + mtx
return(out)
}
new.data <- matrix(1:100, nrow = 10)
benchmark(replications = rep(10000, 1),
noReturn = addNoise(new.data),
withReturn = addNoiseReturn(new.data))
test replications elapsed relative user.self sys.self user.child sys.child
1 noReturn 10000 0.19 1.000 0.19 0 NA NA
2 withReturn 10000 0.22 1.158 0.22 0 NA NA
R
programming, which would be migrated to SO.) However, it's not possible to answer the side question without more information: why are you adding noise to your matrix? What is it intended to represent or model? $\endgroup$lm
run, your attempted "bootstrap" may then give answers that depend arbitrarily on the noise. TheNA
values are a clear sign that the particular bootstrap you are attempting may be invalid or give misleading results. Please consider rethinking your analytical strategy rather than papering over its deficiencies with this artificial fix-up. Perhaps you could instead ask a question in which you describe your data and what you are trying to learn about them, point out the problem with the bootstrap, and ask about alternative approaches. $\endgroup$