【问题标题】:Contingency Table in MathematicaMathematica 中的列联表
【发布时间】:2011-12-13 09:03:28
【问题描述】:

尝试构建我认为的列联表,请考虑以下几点:

dist = Parallelize[Table[RandomVariate[NormalDistribution[]], {100000}]];

dist2 = Rest@FoldList[0.95 # + #2 &, 0, dist];

dist3 = Rest@FoldList[0.95 # + Abs[#2] &, 0, dist];

dist4 = {dist2, dist3}\[Transpose]

q1 = Flatten[{Quantile[dist2, {1/3, 2/3}], Quantile[dist3, {1/3, 2/3}]}]

{-1.39001, 1.33851, 15.0327, 16.6757}

我需要做什么:对于 dist4 的每个元素,我需要查看它所属的下面的 9 个框:

for example : {1.55191, 15.7189} belongs to 2   
                            1.55 belongs to 1 and 
                           15.71 belongs to 8  
So the intersection is 2.  

我尝试过 If 或 Switch,但写起来太长了。有自动的方法吗?

【问题讨论】:

标签: count wolfram-mathematica contingency


【解决方案1】:
y[t_] := Piecewise[{{7, t <  q1[[1]]}, {4, t <= q1[[2]]}}, 1];
x[t_] := Piecewise[{{0, t <  q1[[3]]}, {1, t <= q1[[4]]}}, 2];
{{##}, x[#1] + y[#2]} & @@@ dist4

或者也许使用BinLists!:

k = BinLists[dist4,
   {Join[{Min[dist4[[All, 1]]]}, q1[[1 ;; 2]], {Max[dist4[[All, 1]]]}]},
   {Join[{Min[dist4[[All, 2]]]}, q1[[3 ;; 4]], {Max[dist4[[All, 2]]]}]}
   ];

Flatten[Replace[
        Flatten[MapIndexed[{#1, #2} &, k, {2}], 1], {{x__}, t_} :>
           (Join[{#}, {9 - 3 First@t + Last@t}] & /@ {x}), {1}], 1]

【讨论】:

  • PS:在一个困倦的星期天早上,我可以调整时间吗?我还在喝咖啡。
  • @Verbeia 不,你没有得到任何调整,因为这里仍然是星期六晚上很晚,我什至不会再看到 12 个小时的问题。 ;-)
  • belisarius,为什么你的第一个代码块中有SlotSequence
  • 我想你知道我知道。我试图暗示你混合了方法。无论如何,无论您将/@ 更改为@@@ 还是将## 更改为#,我认为表格应该一致。
  • @Mr.我想你知道我想你知道。我不知道你为什么觉得它错了,但如果是,请做我的客人并纠正它。
【解决方案2】:

使用单独的限制规范:

limits =
  {
   {.1, .15, .5, \[Infinity]},
   {1, 2, 3, \[Infinity]}
   };

cell[l_List] :=
 Table[
  Position[
    limits[[i]], _?(# > l[[i]] &), 1, 1
    ][[1, 1]], {i, 1, Length@l}]

cell[{.4, 1.5}] 然后将产生{3, 2}。您可以使用以下方式进行转换:

(cell[{.4, 1.5}] - {0, 1})*{1, Length[limits[[1]]]} // Plus @@ # &

产生7

【讨论】:

    【解决方案3】:

    您是否考虑过使用阶跃函数? 根据您是否希望在插槽 8 的情况下输出为 {3,2},或者实际上是数字 8,实现可能会有所不同。

    g1[x_] := 
     Piecewise[{{1, x > 1.33851}, {2, 1.33851 >= x > -1.39001}, 3}]
    
    g2[x_] := Piecewise[{{1, x < 15.0327}, {2, 15.0327 <= x < 16.6757}}, 3]
    
    slotfn[{a_, b_}] := {g2[b], g1[a]}
    
    slotnumber[{a_, b_}] := 3 g2[b] + g1[a]
    

    如果贝利撒留的实现真的是一样的,我会删除。请注意,我的非功能性 no-# 版本确保您只将两个参数传递给 slotfnslotnumber

    【讨论】:

      【解决方案4】:

      如果我理解了这个问题,我认为可以这样做:

      {a, b, c, d} = q1;
      tbl = Range@9 ~Partition~ 3;
      
      f[{x_, y_}] := tbl[[
        Which[x > b, 1, x > a, 2, x <= a, 3],
        Which[y < c, 1, y < d, 2, y >= d, 3]
        ]]
      
      f /@ dist4 // Short
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多