統計解析R たぶんpart3くらい (587レス)
統計解析R たぶんpart3くらい http://mevius.5ch.net/test/read.cgi/tech/1340339592/
上
下
前次
1-
新
通常表示
512バイト分割
レス栞
抽出解除
必死チェッカー(本家)
(べ)
自ID
レス栞
あぼーん
リロード規制
です。10分ほどで解除するので、
他のブラウザ
へ避難してください。
393: デフォルトの名無しさん [] 2021/06/20(日) 18:54:38.51 ID:dkTIMvj9 前説が長いので最初に結論: >353は真似しないで欲しい。 [IHaskell](https://github.com/gibiansky/IHaskell)でコードを書く。 ``` haskell import GHC.Exts (groupWith) 分割 = groupWith length 仕事 (a : as) = (length a, length as + 1) 集計 = foldl push mempty where push out a = out ++ pure a 個々 = map 物件 = words "Lorem ipsum dolor sit amet, consectetur adipiscing elit" 比較 前処理 後処理 = lhs == rhs where lhs = 後処理 $ 集計 . 個々 仕事 . 分割 <$> 前処理 物件 rhs = 後処理 $ 集計 <$> 個々 仕事 <$> 分割 <$> 前処理 物件 -- 比較 Identity runIdentity 比較 (\a -> [a, a ++ a]) (flip (>>=) id) 比較 (\a j -> [a !! k | k <- j]) (flip id [1, 3, 5]) ``` [関手](https://en.wikipedia.org/wiki/Functor)と呼ばれるデザインパターンを 使っている。このパターンをRに翻訳する。 ``` {r self, dependson = ""} self = with (new.env (), { id = \(a) a; hom_snd = \(bc) \(ab) \(a) bc (ab (a)); flip = \(abc) \(b) \(a) abc (a) (b); const = \(a) \(b) a; r_curry_fw = \(abc) \(a) \(b) abc (a, b); r_curry_bw = \(abc) \(a, b) abc (a) (b); `%.%` = r_curry_bw (hom_snd); as.list (rlang::current_env ()); }); ``` つづく http://mevius.5ch.net/test/read.cgi/tech/1340339592/393
394: デフォルトの名無しさん [] 2021/06/20(日) 18:56:30.60 ID:dkTIMvj9 例題を次の記事から拝借する。[fmap]{#fmap} * [Split-Apply-Combine and Map-Reduce in R](https://burtmonroe.github.io/SoDA501/Materials/SplitApplyCombine_R/) ``` {r fmap, dependson = "self"} self = with (self, { 分割 = \(x) x |> split (~ cyl); 仕事 = \(x) coef (lm (mpg ~ wt, x)); 集計 = \(x) purrr::map_dbl (x, \(x) x ["wt"]); 個々 = \(f) \(x) purrr::map (x, f); 物件 = mtcars; 比較 = \(管, 前処理, 後処理) with (list (`%管%` = 管), { lhs = 前処理 (物件) %管% (集計 %.% 個々 (仕事) %.% 分割) |> 後処理 (); rhs = 前処理 (物件) %管% 分割 %管% 個々 (仕事) %管% 集計 |> 後処理 (); testthat::test_that ("", testthat::expect_equal (lhs, rhs)); # lookme a = 前処理 (物件); b = 管 (a, 分割); c = 管 (b, 個々 (仕事)); d = 管 (c, 集計); rhs = 後処理 (d); testthat::test_that ("", testthat::expect_equal (lhs, rhs)); }); 比較 (r_curry_bw (flip (id)), \(x) rbind (head (x), tail (x)), id); 比較 (purrr::map, \(x) list (head (x), tail (x)), id); 比較 (r_curry_bw (flip (hom_snd)), \(x) \(y) rbind (head (x), y), \(x) x (tail (物件))); as.list (rlang::current_env ()); }); ``` つづく http://mevius.5ch.net/test/read.cgi/tech/1340339592/394
395: デフォルトの名無しさん [] 2021/06/20(日) 18:58:13.34 ID:dkTIMvj9 1つめの"比較"は表を面倒くさい方法で`lm`している。2つめの"比較"は 表の代わりに表のリストを渡している。この場合の"%管%"が前回の投稿の `%pipe%`に相当する。3つめの"比較"は表の代わりに表への写像を渡している。 このように関手パターンを使うと、引数が様々な"形状"に変化する。 本題に入る。以前、次のようなコードを書いた。[boxplot]{#boxplot} ``` {r, dependson = ""} none = with (new.env (), { . = ggplot2::ggplot (mtcars); . = . + ggplot2::aes (x = factor (cyl), y = wt); . = . + ggplot2::geom_boxplot (); print (.); }); ``` `.`が定数である限り問題はないが、途中一箇所でも`. `が関数になると、 無限ループする。[fmap](#fmap)で`lookme`以下の変数`a..d`を全て`a`にすると、 三番目の"比較"で無限ループする。厄介なことに、全て`a`にしても、 一番目と二番目の"比較"は普通に動作する。関数内の変数は検索経路が 異なることに起因する。 単純化すると、次のコードは動くが、 ``` {r, dependson = ""} tryCatch ({ a = 1; a = 2 + a; a; }, error = identity) |> print (); ``` つづく http://mevius.5ch.net/test/read.cgi/tech/1340339592/395
396: デフォルトの名無しさん [] 2021/06/20(日) 19:00:12.82 ID:dkTIMvj9 次のコードは無限ループする。 ``` {r, dependson = ""} tryCatch ({ a = 1; a = \(.) . + a; a (2); }, error = identity) |> print (); ``` PythonとJSも似たような挙動をするので、現在主流のインタープリターでは [boxplot](#boxplot)のような横着コードはご法度かもしれない。 おしまい http://mevius.5ch.net/test/read.cgi/tech/1340339592/396
メモ帳
(0/65535文字)
上
下
前次
1-
新
書
関
写
板
覧
索
設
栞
歴
スレ情報
赤レス抽出
画像レス抽出
歴の未読スレ
AAサムネイル
Google検索
Wikipedia
ぬこの手
ぬこTOP
0.036s