我有一个矩阵,条目都是概率。大多数条目的概率非常低。有些人有零。我需要记录矩阵。但是,由于矩阵中有零,因此R为这些零条目生成-inf。我的目标是将此日志(矩阵)提供给image.plot()。当我将它提供给image.plot时,我不断收到此错误:
Error in seq.default(minz + binwidth/2, maxz - binwidth/2, by = binwidth) :
invalid (to - from)/by in seq(.)
这里有什么解决方案可以帮助我解决这个问题吗?
这是矩阵的样子:
0 1 2 3 4 5 6
[1,] -0.0007854138 -8.9132811 -10.011893 -10.705041 -9.606428 -9.318746 -Inf
[2,] -0.3402118357 -1.6137090 -2.742625 -4.215836 -5.721434 -7.121522 -9.606428
[3,] -0.2912175507 -2.0296478 -3.521929 -4.275321 -4.426519 -4.187369 -3.715705
[4,] -1.5244380532 -0.7048802 -2.001368 -3.405243 -3.713864 -3.143919 -3.781412
[5,] -0.7572491288 -0.7487709 -3.981208 -5.110329 -5.228577 -5.095569 -5.293395
[6,] -0.0007629648 -Inf -8.759130 -7.613998 -9.606428 -Inf -Inf
[7,] -0.0020658381 -7.4861648 -7.526987 -7.094123 -9.318746 -Inf -Inf
[8,] -0.0295715883 -6.7160566 -7.208533 -6.610696 -6.485533 -6.813220 -6.387552
[9,] -0.0032128722 -6.7160566 -7.613998 -7.871827 -7.760602 -8.759130 -8.759130
[10,] -0.4869248130 -1.3225132 -2.518576 -3.768698 -5.140520 -6.183252 -7.208533
7 8 9
[1,] -Inf -10.705041 -10.011893
[2,] -Inf -Inf -7.149693
[3,] -4.965248 -5.968842 -6.428374
[4,] -4.696227 -5.091913 -4.669559
[5,] -5.163777 -5.468599 -6.577906
[6,] -Inf -Inf -Inf
[7,] -Inf -Inf -Inf
[8,] -6.627503 -6.456545 -6.400976
[9,] -10.011893 -10.011893 -Inf
[10,] -8.402456 -7.814669 -6.546158
这是结构:
structure(c(0.999214894571557, 0.71161956034096, 0.747353073126963,
0.217743382682817, 0.468954688200987, 0.999237326155227, 0.997936294302378,
0.970861372812921, 0.996792283535218, 0.614513234634365, 0.000134589502018843,
0.199147599820547, 0.13138178555406, 0.49416778824585, 0.472947510094213,
0, 0.000560789591745177, 0.00121130551816958, 0.00121130551816958,
0.266464782413638, 4.48631673396142e-05, 0.0644010767160162,
0.0295423956931359, 0.135150291610588, 0.0186630776132795, 0.00015702108568865,
0.00053835800807537, 0.000740242261103634, 0.000493494840735756,
0.0805742485419471, 2.24315836698071e-05, 0.0147599820547331,
0.0139075818752804, 0.0331987438313145, 0.00603409600717811,
0.000493494840735756, 0.000829968595782862, 0.00134589502018843,
0.000381336922386721, 0.0230820995962315, 6.72947510094213e-05,
0.00327501121579183, 0.0119560340960072, 0.0243831314490803,
0.00536114849708389, 6.72947510094213e-05, 8.97263346792284e-05,
0.00152534768954688, 0.000426200089726335, 0.00585464333781965,
8.97263346792284e-05, 0.000807537012113055, 0.0151861821444594,
0.0431135038133692, 0.00612382234185734, 0, 0, 0.00109914759982055,
0.00015702108568865, 0.00206370569762225, 0, 6.72947510094213e-05,
0.0243382682817407, 0.022790489008524, 0.00502467474203679, 0,
0, 0.00168236877523553, 0.00015702108568865, 0.000740242261103634,
0, 0, 0.00697622252131, 0.00912965455361149, 0.00572005383580081,
0, 0, 0.00132346343651862, 4.48631673396142e-05, 0.000224315836698071,
2.24315836698071e-05, 0, 0.00255720053835801, 0.00614625392552714,
0.00421713772992373, 0, 0, 0.0015702108568865, 4.48631673396142e-05,
0.000403768506056528, 4.48631673396142e-05, 0.000785105428443248,
0.00161507402422611, 0.00937640197397936, 0.00139075818752804,
0, 0, 0.00165993719156572, 0, 0.00143562135486765), .Dim = c(10L,
10L), .Dimnames = list(NULL, c("0", "1", "2", "3", "4", "5",
"6", "7", "8", "9")))
答案 0 :(得分:4)
如果这些零点是由物理测量引起的,它应该产生肯定的结果,但由于技术原因而不能这样做,那么将零点检测的下限替换为0可能是合理的。
M2 <- M
print( min(M[M!=0]), digits=16)
#[1] 2.24315836698071e-05
M2[M2==0] <- 0.5*min(M[M!=0])
image(M2)
image(log(M2))
答案 1 :(得分:2)
一个简单的技巧是从log1 = 0开始加1,这样0后的单元格在转换后仍然会有0。
k<-matrix(c(1:8,0,0),nrow=2,ncol=5)
> k
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 0
[2,] 2 4 6 8 0
log(k)
[,1] [,2] [,3] [,4] [,5]
[1,] 0.0000000 1.098612 1.609438 1.945910 -Inf
[2,] 0.6931472 1.386294 1.791759 2.079442 -Inf
log(k+1)
[,1] [,2] [,3] [,4] [,5]
[1,] 0.6931472 1.386294 1.791759 2.079442 0
[2,] 1.0986123 1.609438 1.945910 2.197225 0
答案 2 :(得分:2)
seq()
抛出了except,它不能将-inf
作为其任何一个参数。您可以使用以下代码获得完全相同类型的错误:
> seq(-log(0), 0, 50)
Error in seq.default(-log(0), 0, 50) : invalid (to - from)/by in seq(.)
要避免它,请遵循@Metrics的诀窍。虽然我建议不添加1.0,但添加一个非常小的值,例如1e-22,因为你的矩阵是一个概率矩阵。
答案 3 :(得分:2)
确实,日志图可能会使“条目之间的差异更明显”。但是,如果您的数据中有零,则表示您使用的是错误的。对数标度的点是为了说明数据的指数增长。但是,有零意味着:
无论哪种方式,在你的情况下更好的方法是使用值的平方根。或者(n> 2) - 根如果你想要更加强调值的差异 - n 越高,差异越大。
根据@ flodel的建议,执行此操作的代码为:image.plot(sqrt(x))
,或者更常见的是,某些image.plot(x^(1/n))
的{{1}}。
希望这有帮助。
答案 4 :(得分:0)
无法在评论中粘贴多行代码,但此示例显示了我的意思:
> m=cbind(c(0,0.88,0.99),c(1,2,1),c(3,4,5))
> m=as.matrix(m)
> log(m)
[,1] [,2] [,3]
[1,] -Inf 0.0000000 1.098612
[2,] -0.12783337 0.6931472 1.386294
[3,] -0.01005034 0.0000000 1.609438
> m
[,1] [,2] [,3]
[1,] 0.00 1 3
[2,] 0.88 2 4
[3,] 0.99 1 5