Runif生成NaN术语

时间:2016-01-05 23:57:49

标签: r if-statement matrix

我有一个不同参数的基线值矩阵,它是1列x 305行矩阵,我想用runif生成一个随机数矩阵(每个参数值100个)+ - 初始参数值的10%。问题是当不应该有输出时随机散布NaN个术语,我收到此警告信息:

Warning message:
In runif(n = 30500, min = lower, max = upper) : NAs produced

到目前为止,这是我的代码:

baseline <- scan("baseline.txt")
baseline <- as.matrix(baseline)
base <- t(baseline)
upper <- 1.1 * base
lower <- 0.9 * base
matrice2 <- matrix(runif(n=30500, min = lower, max = upper), nrow=305) 

(我必须使用t(baseline),以便生成的matrice2以正确的形式传递给我稍后使用的另一个函数。基线数字范围为-0.281至800.

任何人都有任何关于它为什么会产生NaN的想法?在假期休假之前,我正在使用这个确切的代码,然后就可以了。

编辑:这是baseline.txt数据,305行x 1列文本文件

baseline
                [,1]
  [1,]   0.400000000
  [2,]   0.800000000
  [3,]   0.200000000
  [4,]   0.800000000
  [5,]   0.600000000
  [6,]   0.800000000
  [7,]   0.150000000
  [8,]   0.900000000
  [9,]   0.500000000
 [10,]   0.600000000
 [11,]   0.500000000
 [12,]   0.500000000
 [13,]   0.600000000
 [14,]   0.500000000
 [15,]   0.500000000
 [16,]   0.600000000
 [17,]   0.200000000
 [18,]   0.700000000
 [19,]   0.800000000
 [20,]   0.300000000
 [21,]   0.700000000
 [22,]   0.800000000
 [23,]   0.300000000
 [24,]   0.700000000
 [25,]   0.800000000
 [26,]   0.200000000
 [27,]   0.002000000
 [28,]   0.400000000
 [29,]   0.001000000
 [30,]   0.001300000
 [31,]   0.800000000
 [32,]   0.526000000
 [33,]   0.526000000
 [34,]   0.714000000
 [35,]   0.306000000
 [36,]   0.306000000
 [37,]   0.711000000
 [38,]   0.711000000
 [39,]   0.847000000
 [40,]   0.300000000
 [41,]   0.300000000
 [42,]   0.300000000
 [43,]   0.300000000
 [44,]   0.300000000
 [45,]   0.300000000
 [46,]   0.050000000
 [47,]   0.400000000
 [48,]   0.450000000
 [49,]   0.300000000
 [50,]   0.400000000
 [51,]   0.700000000
 [52,]   0.400000000
 [53,]   0.600000000
 [54,]   0.306000000
 [55,]   0.885000000
 [56,]   0.008610000
 [57,]   0.008610000
 [58,]   0.008610000
 [59,]   0.008610000
 [60,]   0.016500000
 [61,]   6.450000000
 [62,]   6.450000000
 [63,]   6.450000000
 [64,]   6.450000000
 [65,]   0.014650000
 [66,]   0.014650000
 [67,]   0.014650000
 [68,]   0.014650000
 [69,]   0.200000000
 [70,]   0.400000000
 [71,]   0.400000000
 [72,]   3.000000000
 [73,]   3.000000000
 [74,]   3.000000000
 [75,]   3.000000000
 [76,]   3.000000000
 [77,]   3.000000000
 [78,]   3.000000000
 [79,]   3.000000000
 [80,]   3.000000000
 [81,]   0.338000000
 [82,]   0.013200000
 [83,]   0.001350000
 [84,]   0.001350000
 [85,]   0.001350000
 [86,]   0.338000000
 [87,]  34.694000000
 [88,]   0.002710000
 [89,]   0.000420000
 [90,]   0.015900000
 [91,]   0.037000000
 [92,]   0.024500000
 [93,]   0.075000000
 [94,]   0.045000000
 [95,]   0.024800000
 [96,]   0.015900000
 [97,]   0.045000000
 [98,]   0.045000000
 [99,]   2.800000000
[100,]   0.001590000
[101,]   0.004500000
[102,]   0.001000000
[103,]   0.020000000
[104,]   0.004800000
[105,]   7.500000000
[106,]  34.694000000
[107,]   0.338000000
[108,]   0.028900000
[109,]   0.022400000
[110,]   0.022400000
[111,]   0.040000000
[112,]   0.008000000
[113,]   0.338000000
[114,]   0.026400000
[115,]   0.046080000
[116,]   0.057600000
[117,]   0.700000000
[118,]   0.400000000
[119,]   0.200000000
[120,]   0.700000000
[121,]   0.300000000
[122,]   0.750000000
[123,]   4.000000000
[124,]   0.500000000
[125,]   8.000000000
[126,] 800.000000000
[127,]   3.000000000
[128,]   3.000000000
[129,]   6.480000000
[130,]   1.150000000
[131,]   0.700000000
[132,]   0.100000000
[133,]   1.793000000
[134,]   1.291000000
[135,]   1.793000000
[136,]   1.291000000
[137,]   1.793000000
[138,]   1.291000000
[139,]   5.944000000
[140,]   4.877000000
[141,]   0.000210000
[142,]   0.006700000
[143,]   2.000000000
[144,]   0.080000000
[145,]   0.110000000
[146,]   0.008500000
[147,] 120.000000000
[148,]  60.000000000
[149,]   3.200000000
[150,]   0.800000000
[151,]   1.300000000
[152,]   1.300000000
[153,]   1.300000000
[154,]   2.690000000
[155,]   0.024500000
[156,]   7.850000000
[157,]   1.500000000
[158,]  10.000000000
[159,]  10.000000000
[160,]   4.300000000
[161,]   0.050000000
[162,]   0.400000000
[163,]   7.860000000
[164,]   3.000000000
[165,]   0.052800000
[166,]   0.052800000
[167,]  15.439000000
[168,]   7.860000000
[169,]  15.439000000
[170,]   0.200000000
[171,]   0.053000000
[172,]   0.022000000
[173,]   0.164600000
[174,]   0.164600000
[175,]   0.164600000
[176,]   0.164600000
[177,]   4.100000000
[178,]  15.439000000
[179,]   7.860000000
[180,]   0.160000000
[181,]   0.040000000
[182,]   0.014400000
[183,]   7.860000000
[184,]   6.000000000
[185,]   0.300000000
[186,]   0.300000000
[187,]  33.500000000
[188,]   0.020000000
[189,]   1.277000000
[190,]   0.981700000
[191,]   1.000000000
[192,]   1.000000000
[193,]   1.000000000
[194,]   0.016666667
[195,]   0.271000000
[196,]   0.504000000
[197,]   0.504000000
[198,]   0.504000000
[199,]   0.713000000
[200,]   0.742000000
[201,]   0.713000000
[202,]   0.981700000
[203,]   0.058800000
[204,]   1.257000000
[205,]   1.257000000
[206,]   2.723000000
[207,]   3.969000000
[208,]   2.462000000
[209,]   2.492000000
[210,]   3.000000000
[211,]   0.815000000
[212,]   1.191000000
[213,]   1.191000000
[214,]   1.191000000
[215,]   2.241000000
[216,]   3.969000000
[217,]   2.241000000
[218,]   3.969000000
[219,] 152.000000000
[220,]   0.085000000
[221,]   0.493900000
[222,]   0.231000000
[223,]   0.246000000
[224,]   0.500000000
[225,]   0.011363636
[226,]   0.133000000
[227,]   0.133000000
[228,]   0.133000000
[229,]   0.198000000
[230,]   0.493900000
[231,]   0.198000000
[232,]   0.493900000
[233,]   1.447000000
[234,]   1.970000000
[235,]   0.995000000
[236,]   0.995000000
[237,]   1.000000000
[238,]   0.271000000
[239,]   0.623000000
[240,]   0.623000000
[241,]   0.623000000
[242,]   1.128000000
[243,]   1.970000000
[244,]   1.128000000
[245,]   1.970000000
[246,]   2.256000000
[247,]   2.951000000
[248,]   3.000000000
[249,]   3.000000000
[250,]   3.000000000
[251,]  -0.281000000
[252,]   1.065000000
[253,]   1.065000000
[254,]   1.065000000
[255,]   0.949000000
[256,]   2.951000000
[257,]   0.949000000
[258,]   2.951000000
[259,]   0.000840000
[260,]   0.000120000
[261,]   0.006200000
[262,]   0.006200000
[263,]   0.006200000
[264,]   0.085000000
[265,]   0.030500000
[266,]   0.459000000
[267,]   0.030500000
[268,]   0.459000000
[269,]   0.030500000
[270,]   0.011100000
[271,]   0.001481000
[272,]   0.000210000
[273,]   0.000210000
[274,]   0.000210000
[275,]   0.000210000
[276,] 142.050000000
[277,]   0.468000000
[278,]   0.355000000
[279,]   0.335000000
[280,]   0.013513514
[281,]   0.729000000
[282,]   0.154000000
[283,]   0.154000000
[284,]   0.154000000
[285,]   0.396000000
[286,]   0.396000000
[287,]   0.009100000
[288,]   0.006700000
[289,]   0.006700000
[290,]   0.006700000
[291,]   0.006700000
[292,] 112.500000000
[293,] 112.500000000
[294,] 112.500000000
[295,]   0.009803922
[296,]   0.237000000
[297,]   0.237000000
[298,]   0.237000000
[299,]   0.017900000
[300,]   0.017900000
[301,]   0.000120000
[302,]   0.000120000
[303,]   0.000120000
[304,]   0.006200000
[305,]   0.006200000

1 个答案:

答案 0 :(得分:6)

正如评论中指出的那样,runif min必须小于max。你计算最​​小值和最大值的方法将最大值从0移动到最远,并且分钟越接近于0,这只有在数字为正时才有效。

正如评论中所建议的,有几种方法可以做到这一点,例如pminpmax

upper = pmax(0.9 * base, 1.1 * base)
lower = pmin(0.9 * base, 1.1 * base)

另一种可能更有效的方式是:

offset = 0.1 * abs(base)
upper = base + offset
lower = base - offset