Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

distMat.KernelKnn returns a matrix of zeros when argument weights_function is specified. #5

Closed
mmp3 opened this issue Sep 27, 2021 · 5 comments

Comments

@mmp3
Copy link

mmp3 commented Sep 27, 2021

If I specify the weights_function argument to distMat.KernelKnn, then I get a matrix of all zeros.

Using the same example as in issue #4 but with test data last (to avoid the bug described in #4):

dmat <- read.csv(text = "0,0.14708019618331958,0.25379467499774966,0.25259357887665446,0.24411040124607417,0.37952218492356554,0.22128441502163412,0.21484977525596705,0.45660763074957084,0.30977361740258297,0.5363408107197073,0.40489626366258386,0.4972366668596603,0.3064613800372366,0.24895048851400128,0.2609645105097838,0.314074088063201,0.3123197830342413,0.45627096900511244,0.2512579526351698,0.24532963098718388,0.5190339711715779,0.4788521132853014,0.2548306722891173,0.3320514371434748,0.36552119504407987
0.14708019618331958,0,0.2379725936798857,0.24371648093535592,0.22773819610452836,0.3840230645530348,0.19765299631230465,0.19143701905565919,0.4312739477810525,0.30666084751602113,0.5167001131798435,0.4084205257276984,0.5141620916108083,0.29120990978376893,0.23698898766640536,0.24654860634965006,0.3069814058198148,0.2900125993238617,0.429471064205015,0.2381343100132991,0.23702253832185882,0.5007872593496107,0.48662322886403137,0.24993773886046922,0.3316016293170049,0.348119277342493
0.25379467499774966,0.2379725936798857,0,0.17984303486010553,0.09243160048429888,0.3063638131891691,0.08986902862030387,0.10607794242879076,0.4292957202520237,0.26316280254274377,0.4133195226240847,0.4278669834391728,0.5519293828408495,0.14359353160546506,0.08588588011838162,0.045017576388234296,0.14992520911491178,0.28281909952806383,0.3848904198415885,0.08829030000165292,0.08538759072042186,0.41123710113984,0.49905168189829524,0.14262716998887717,0.2549290781813745,0.5487801635344882
0.25259357887665446,0.24371648093535592,0.17984303486010553,0,0.16797240705095748,0.2495539608494193,0.16162227688935157,0.1583571009161883,0.4744251229637033,0.2614969911856393,0.5268575555419442,0.34776087804910766,0.44042198243008185,0.2761723716903082,0.16794559423177713,0.1808579353281036,0.28173347149198663,0.3069564507932346,0.46516561991761557,0.1695036905538969,0.16048012917353488,0.5127914965504357,0.4152992935698232,0.13607294280815252,0.16993310886329108,0.4508200464198177
0.24411040124607417,0.22773819610452836,0.09243160048429888,0.16797240705095748,0,0.2664217988865222,0.08726591399599148,0.10695724769854562,0.3887827107036979,0.24655576122468784,0.4138625165443031,0.3887206094614384,0.5304054048120482,0.16058430597010306,0.0941046020472153,0.10367354053337763,0.18201535396579502,0.26006531568590713,0.34806313525623434,0.09555336773652591,0.0968486911009125,0.376134839837546,0.4946442071132992,0.1056713315199139,0.21997007941457672,0.5165877398634016
0.37952218492356554,0.3840230645530348,0.3063638131891691,0.2495539608494193,0.2664217988865222,0,0.2870284454142751,0.28579308415321414,0.6096336746281601,0.38934380299398985,0.6688859695189381,0.49545639598255475,0.47531582143238993,0.43885795643497194,0.28671089381847853,0.3043647254296273,0.42391491100319256,0.44309586909470716,0.6149775853720031,0.2855572728421659,0.27425752231934825,0.6637913324876132,0.3089141832011178,0.23089310509679534,0.1734811691523492,0.5966911402425906
0.22128441502163412,0.19765299631230465,0.08986902862030387,0.16162227688935157,0.08726591399599148,0.2870284454142751,0,0.06197013181116337,0.309063924436977,0.2104769968061645,0.36997984358634844,0.3699097454419498,0.5277522669990433,0.16478152925948628,0.11117274101746084,0.1037180646763772,0.19251652839358366,0.2034130499454453,0.2742431781907229,0.11172886690189733,0.11151790887078833,0.31815546931913047,0.4789137157239134,0.1380961756208064,0.25141809563183326,0.4671862848818978
0.21484977525596705,0.19143701905565919,0.10607794242879076,0.1583571009161883,0.10695724769854562,0.28579308415321414,0.06197013181116337,0,0.33826329742966404,0.19653085335941134,0.36097036158248513,0.3552125196364281,0.5062418606064927,0.1773460235282751,0.11241982565125397,0.11418983457736596,0.19680666912271572,0.21535647739761918,0.32039202517822696,0.11271137355454076,0.11153937187905635,0.33730633434214125,0.460914329521266,0.13807976678442174,0.23927914166553202,0.43395258595656916
0.45660763074957084,0.4312739477810525,0.4292957202520237,0.4744251229637033,0.3887827107036979,0.6096336746281601,0.309063924436977,0.33826329742966404,0,0.4323196239087167,0.46696024817825893,0.4580316647308767,0.5554212273489155,0.4426250009145398,0.44455359672627726,0.45590300459067723,0.47214879048773656,0.16844139680843884,0.09246129184764731,0.435642898284773,0.4479738124637136,0.4263310227351765,0.6761341977059867,0.4701055860873319,0.5767720592350046,0.5217739102011146
0.30977361740258297,0.30666084751602113,0.26316280254274377,0.2614969911856393,0.24655576122468784,0.38934380299398985,0.2104769968061645,0.19653085335941134,0.4323196239087167,0,0.33576855733575406,0.3344170322557916,0.46416314580532264,0.3131369555462586,0.25239703850416884,0.27382323722642765,0.3082673911459641,0.29431795742903977,0.4211911505178604,0.24984321044204738,0.2483194497793813,0.2739507037353858,0.4873635872164822,0.26047830112556464,0.34231987031742994,0.468783458185031
0.5363408107197073,0.5167001131798435,0.4133195226240847,0.5268575555419442,0.4138625165443031,0.6688859695189381,0.36997984358634844,0.36097036158248513,0.46696024817825893,0.33576855733575406,0,0.4979036521501042,0.6104804379939913,0.4745947341809774,0.4474122706757421,0.43008941046579696,0.4942764787901638,0.45096267220127634,0.45225567088506025,0.43938923835915583,0.45521348178943455,0.2643270415486569,0.7213679571444382,0.5037639709146426,0.625478775334365,0.5580575619972978
0.40489626366258386,0.4084205257276984,0.4278669834391728,0.34776087804910766,0.3887206094614384,0.49545639598255475,0.3699097454419498,0.3552125196364281,0.4580316647308767,0.3344170322557916,0.4979036521501042,0,0.2540082751713078,0.4305089445243082,0.40686736218226205,0.43833134976590427,0.4368022850161604,0.3757780274654418,0.45394933985339425,0.40247217385173756,0.4014504125813846,0.49038771411046445,0.4449082410467523,0.38874088642201643,0.44180742881694635,0.49522710849468593
0.4972366668596603,0.5141620916108083,0.5519293828408495,0.44042198243008185,0.5304054048120482,0.47531582143238993,0.5277522669990433,0.5062418606064927,0.5554212273489155,0.46416314580532264,0.6104804379939913,0.2540082751713078,0,0.5393816502718939,0.5174582193999983,0.5522089819271963,0.5364617953289816,0.48449738486849403,0.5646486325642321,0.510905425913238,0.5085758506434891,0.6082986824290273,0.40518625816752213,0.5009973114972146,0.45190733665745897,0.5491428597402045
0.3064613800372366,0.29120990978376893,0.14359353160546506,0.2761723716903082,0.16058430597010306,0.43885795643497194,0.16478152925948628,0.1773460235282751,0.4426250009145398,0.3131369555462586,0.4745947341809774,0.4305089445243082,0.5393816502718939,0,0.13832965607653436,0.1194748645814226,0.10226012281644359,0.30208742031089875,0.41002797979135597,0.13796168809008763,0.1445796795459895,0.44562464408473274,0.5801996541377322,0.21675452805762405,0.3722124205784203,0.5377141953382527
0.24895048851400128,0.23698898766640536,0.08588588011838162,0.16794559423177713,0.0941046020472153,0.28671089381847853,0.11117274101746084,0.11241982565125397,0.44455359672627726,0.25239703850416884,0.4474122706757421,0.40686736218226205,0.5174582193999983,0.13832965607653436,0,0.07713746628509803,0.13240279743497624,0.2873458051726545,0.4203110877085595,0.012876205034978492,0.011854131899621138,0.41928852860907095,0.4840523261422458,0.12099408829723136,0.23126880560569246,0.5177613091497286
0.2609645105097838,0.24654860634965006,0.045017576388234296,0.1808579353281036,0.10367354053337763,0.3043647254296273,0.1037180646763772,0.11418983457736596,0.45590300459067723,0.27382323722642765,0.43008941046579696,0.43833134976590427,0.5522089819271963,0.1194748645814226,0.07713746628509803,0,0.11517906166164485,0.2997832529510705,0.42307347507313603,0.0803447366991365,0.07695548084340992,0.4344821175006896,0.4975028732751139,0.13864495406725313,0.2513839176585469,0.5565538558957703
0.314074088063201,0.3069814058198148,0.14992520911491178,0.28173347149198663,0.18201535396579502,0.42391491100319256,0.19251652839358366,0.19680666912271572,0.47214879048773656,0.3082673911459641,0.4942764787901638,0.4368022850161604,0.5364617953289816,0.10226012281644359,0.13240279743497624,0.11517906166164485,0,0.32554644694071594,0.44887295347478995,0.13643922915416956,0.1331679852899057,0.4715898654143029,0.5704013551405627,0.218747455025007,0.36188612454854363,0.5322453054261741
0.3123197830342413,0.2900125993238617,0.28281909952806383,0.3069564507932346,0.26006531568590713,0.44309586909470716,0.2034130499454453,0.21535647739761918,0.16844139680843884,0.29431795742903977,0.45096267220127634,0.3757780274654418,0.48449738486849403,0.30208742031089875,0.2873458051726545,0.2997832529510705,0.32554644694071594,0,0.15202923960395442,0.28163328004602894,0.28917610306004304,0.4081303600252849,0.538806952709379,0.31021865502374996,0.4122006391321491,0.45970523229460947
0.45627096900511244,0.429471064205015,0.3848904198415885,0.46516561991761557,0.34806313525623434,0.6149775853720031,0.2742431781907229,0.32039202517822696,0.09246129184764731,0.4211911505178604,0.45225567088506025,0.45394933985339425,0.5646486325642321,0.41002797979135597,0.4203110877085595,0.42307347507313603,0.44887295347478995,0.15202923960395442,0,0.4107289487324347,0.4257570128664347,0.41064104524846423,0.6848900987234641,0.4557653691375644,0.5787509133993745,0.5306700547723552
0.2512579526351698,0.2381343100132991,0.08829030000165292,0.1695036905538969,0.09555336773652591,0.2855572728421659,0.11172886690189733,0.11271137355454076,0.435642898284773,0.24984321044204738,0.43938923835915583,0.40247217385173756,0.510905425913238,0.13796168809008763,0.012876205034978492,0.0803447366991365,0.13643922915416956,0.28163328004602894,0.4107289487324347,0,0.014165320888756366,0.4141943435433569,0.47928507880504306,0.12548748811574206,0.232085171184312,0.5168787735456748
0.24532963098718388,0.23702253832185882,0.08538759072042186,0.16048012917353488,0.0968486911009125,0.27425752231934825,0.11151790887078833,0.11153937187905635,0.4479738124637136,0.2483194497793813,0.45521348178943455,0.4014504125813846,0.5085758506434891,0.1445796795459895,0.011854131899621138,0.07695548084340992,0.1331679852899057,0.28917610306004304,0.4257570128664347,0.014165320888756366,0,0.4285987212791397,0.4748462380155547,0.11588607566058462,0.2214973697274498,0.5105306711163828
0.5190339711715779,0.5007872593496107,0.41123710113984,0.5127914965504357,0.376134839837546,0.6637913324876132,0.31815546931913047,0.33730633434214125,0.4263310227351765,0.2739507037353858,0.2643270415486569,0.49038771411046445,0.6082986824290273,0.44562464408473274,0.41928852860907095,0.4344821175006896,0.4715898654143029,0.4081303600252849,0.41064104524846423,0.4141943435433569,0.4285987212791397,0,0.7225133446282607,0.48476072038177914,0.6169667267435115,0.5579542014614032
0.4788521132853014,0.48662322886403137,0.49905168189829524,0.4152992935698232,0.4946442071132992,0.3089141832011178,0.4789137157239134,0.460914329521266,0.6761341977059867,0.4873635872164822,0.7213679571444382,0.4449082410467523,0.40518625816752213,0.5801996541377322,0.4840523261422458,0.4975028732751139,0.5704013551405627,0.538806952709379,0.6848900987234641,0.47928507880504306,0.4748462380155547,0.7225133446282607,0,0.46473250308317626,0.38385035731120953,0.6547382946289004
0.2548306722891173,0.24993773886046922,0.14262716998887717,0.13607294280815252,0.1056713315199139,0.23089310509679534,0.1380961756208064,0.13807976678442174,0.4701055860873319,0.26047830112556464,0.5037639709146426,0.38874088642201643,0.5009973114972146,0.21675452805762405,0.12099408829723136,0.13864495406725313,0.218747455025007,0.31021865502374996,0.4557653691375644,0.12548748811574206,0.11588607566058462,0.48476072038177914,0.46473250308317626,0,0.142982674797056,0.4859099987326353
0.3320514371434748,0.3316016293170049,0.2549290781813745,0.16993310886329108,0.21997007941457672,0.1734811691523492,0.25141809563183326,0.23927914166553202,0.5767720592350046,0.34231987031742994,0.625478775334365,0.44180742881694635,0.45190733665745897,0.3722124205784203,0.23126880560569246,0.2513839176585469,0.36188612454854363,0.4122006391321491,0.5787509133993745,0.232085171184312,0.2214973697274498,0.6169667267435115,0.38385035731120953,0.142982674797056,0,0.542091628045374
0.36552119504407987,0.348119277342493,0.5487801635344882,0.4508200464198177,0.5165877398634016,0.5966911402425906,0.4671862848818978,0.43395258595656916,0.5217739102011146,0.468783458185031,0.5580575619972978,0.49522710849468593,0.5491428597402045,0.5377141953382527,0.5177613091497286,0.5565538558957703,0.5322453054261741,0.45970523229460947,0.5306700547723552,0.5168787735456748,0.5105306711163828,0.5579542014614032,0.6547382946289004,0.4859099987326353,0.542091628045374,0", header=FALSE)

dmat <- as.matrix(dmat)

a <- KernelKnn::distMat.KernelKnn(
          DIST_mat= dmat ,
          TEST_indices = 12:26 ,
          y= c(1,1,3,3,4,4,5,6,7,7,8),
          Levels= letters[1:8] ,
          k=2)

b <- KernelKnn::distMat.KernelKnn(
          DIST_mat= dmat ,
          TEST_indices = 12:26 ,
          y= c(1,1,3,3,4,4,5,6,7,7,8),
          Levels= letters[1:8] ,
          k=2 ,
          weights_function = "uniform" )

a is correct, but b gives a zero matrix:

> show(a)
      class_a class_b class_c class_d class_e class_f class_g class_h
 [1,]       0       0     0.5     0.0     0.0       0     0.5     0.0
 [2,]       0       0     0.5     0.0     0.0       0     0.5     0.0
 [3,]       0       0     0.5     0.5     0.0       0     0.0     0.0
 [4,]       0       0     0.5     0.5     0.0       0     0.0     0.0
 [5,]       0       0     0.5     0.5     0.0       0     0.0     0.0
 [6,]       0       0     0.5     0.5     0.0       0     0.0     0.0
 [7,]       0       0     0.0     0.0     0.5       0     0.5     0.0
 [8,]       0       0     0.0     0.0     0.5       0     0.5     0.0
 [9,]       0       0     0.5     0.5     0.0       0     0.0     0.0
[10,]       0       0     0.5     0.5     0.0       0     0.0     0.0
[11,]       0       0     0.0     0.0     0.0       0     0.5     0.5
[12,]       0       0     0.5     0.5     0.0       0     0.0     0.0
[13,]       0       0     0.5     0.5     0.0       0     0.0     0.0
[14,]       0       0     0.5     0.5     0.0       0     0.0     0.0
[15,]       1       0     0.0     0.0     0.0       0     0.0     0.0


> show(b)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
 [1,]    0    0    0    0    0    0    0    0
 [2,]    0    0    0    0    0    0    0    0
 [3,]    0    0    0    0    0    0    0    0
 [4,]    0    0    0    0    0    0    0    0
 [5,]    0    0    0    0    0    0    0    0
 [6,]    0    0    0    0    0    0    0    0
 [7,]    0    0    0    0    0    0    0    0
 [8,]    0    0    0    0    0    0    0    0
 [9,]    0    0    0    0    0    0    0    0
[10,]    0    0    0    0    0    0    0    0
[11,]    0    0    0    0    0    0    0    0
[12,]    0    0    0    0    0    0    0    0
[13,]    0    0    0    0    0    0    0    0
[14,]    0    0    0    0    0    0    0    0
[15,]    0    0    0    0    0    0    0    0
@mlampros
Copy link
Owner

@mmp3,

the "Levels" parameter must correspond to the unique classes of "y" in your data but you specified as "Levels" the letters[1:8], that's why you received an all zero matrix. The following works but make sure that you pass the unique y class levels which I guess is 1:8

y = c(1,1,3,3,4,4,5,6,7,7,8)

b <- KernelKnn::distMat.KernelKnn(
  DIST_mat= dmat ,
  TEST_indices = 12:26 ,
  y= y,
  Levels= 1:8 ,
  k=2 ,
  weights_function = "uniform" )

@mlampros
Copy link
Owner

I'll have to include an error in that case. I'll do that in a few days

@mmp3
Copy link
Author

mmp3 commented Sep 28, 2021

Oh yes, I see now in the documentation - both y and Levels are numeric vectors. My mistake!

That it worked fine when I gave a character vector to Levels (and weights_function = NULL) made it easier to overlook the documentation for Levels...

@mlampros
Copy link
Owner

mlampros commented Oct 5, 2021

@mmp3,
the updated version is currently on CRAN (1.1.3)

@mmp3
Copy link
Author

mmp3 commented Oct 8, 2021

Great.
Thank you, @mlampros!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants