R Weekly Highlights - Issue 2026-W18 Highlights 封面

Issue 2026-W18 Highlights

Issue 2026-W18 Highlights

本集简介

How the new logrittr package gives you a peek behind the tidy pipelines of dplyr, and a fascinating glimpse into the universe of seeds going far beyond a favorite 80's song. Episode Links This week's curator: Jonathan Kitt - @jonathankitt.bsky.social (Bluesky) logrittr: A Verbose Pipe Operator for Logging dplyr Pipelines Generating universes within universes with a single seed Working Smarter in R: Tips, Tricks & Real-World Lessons Entire issue available at rweekly.org/2026-W18 Supplement Resources tidylog https://github.com/elbersb/tidylog/ logrittr documentation https://guillaumepressiat.github.io/logrittr/ Fine-grained control of RNG seeds in R https://blog.djnavarro.net/posts/2023-12-27_seedcatcher Supporting the show Use the contact page at https://serve.podhome.fm/custompage/r-weekly-highlights/contact to send us your feedback R-Weekly Highlights on the Podcastindex.org - You can send a boost into the show directly in the Podcast Index. First, top-up with Alby, and then head over to the R-Weekly Highlights podcast entry on the index. A new way to think about value: https://value4value.info Get in touch with us on social media Eric Nantz: @rpodcast@podcastindex.social (Mastodon), @rpodcast.bsky.social (BlueSky) and @theRcast (X/Twitter) Mike Thomas: @mike_thomas@fosstodon.org (Mastodon), @mike-thomas.bsky.social (BlueSky), and @mike_ketchbrook (X/Twitter) Music credits powered by OCRemix Passing Breeze (Lounge Suite) - OutRun - Remorse - https://ocremix.org/remix/OCR00875

双语字幕

仅展示文本字幕,不包含中文音频;想边听边看,请使用 Bayt 播客 App。

Speaker 0

大家好,欢迎回到《每周亮点》播客第224期,这是我们每周固定节目,专门分享rweekly.org上那些精彩内容和资源。顺便提个通知,之前我说过我们要坚持每周准时更新,但上周没出新一期,所以停了一周;不过这周新内容来了,咱们又回来啦!

Hello friends, we are back of episode two twenty four of the Our Weekly Highlights Podcast and this is the usually weekly show where we talk about the great highlights and other resources shared at rweekly.org and just a quick admin note as much as I was saying we're going to try and be consistent with our week to week release unfortunately we did not have an issue last week so we were off but we got a new issue this week so we are back.

Speaker 0

我是埃里克·纳恩茨,很高兴无论你身在世界哪个角落,今天都能和我们在一起。和往常一样,线上和我一起主持的是迈克·托马斯。

My name is Eric Nantz and I'm delighted that you joined us from wherever you are around the world and joining me virtually as always is Mike Thomas.

Speaker 0

迈克,你今天怎么样?

Mike, how are you doing today?

Speaker 1

还不错,埃里克,就是春天过敏有点闹腾,不过我吃了非索非那定,硬撑着过来了。

Doing pretty well, Eric, fighting the spring allergies but got some, phalonase going and got to power through this.

Speaker 0

是啊,我这会儿也感觉像到了周中低谷期。

Yeah, I've got feel like I got that midweek here.

Speaker 0

跟一个月前生病不太一样,但足够让我意识到——季节真换了,霉菌满天飞,下次我要是去割草,估计得打五十个喷嚏。

It's different than when I was sick like a month ago but it's enough to be like, yep the seasons change, the molds are out there and yeah, next time I have to mow the lawn, I'm probably going to sneeze 50 times.

Speaker 0

但这就是生活嘛,属于发达国家的烦恼。

But it is what it is, first world problems,

Speaker 1

大概吧。

I guess.

Speaker 0

情况本来可能更糟,不过今天我们来聊点积极的

Could be a lot worse, but we're going talk about

Speaker 1

没错,说正经的,

positive Yeah,

Speaker 0

今天咱们聊点正能量的事儿。

about positive things today.

Speaker 0

这期的精选内容特别棒,本周的整理人是乔纳森·基特,像往常一样,他得到了我们RWeekly团队成员和全球各位贡献者的大力协助——你们通过投票和建议,让这一切成为可能。

We got a great selection of highlights and our curator for this week's issue is Jonathan Kitt, and as always he had tremendous help from our fellow rweekly team members and contributors like all of you around the world with your poll requests and other suggestions.

Speaker 0

咱们先从一个在tidyverse和数据处理领域特别实用的小工具说起,它能让你一窥那些通常藏在后台、或者至少是通过类似dplyr这样的管道操作时发生的事。

And we're going to lead off with a great utility in the land of the tidyverse and data processing and getting a glimpse into kind of what's happening maybe behind the scenes or at least through a typical, you know, dplyr like pipeline.

Speaker 0

我相信在座的各位都熟悉tidyverse,尤其是dplyr,它让你能轻松创建新变量、筛选数据、做各种转换,早就成了数据科学和数据清洗领域的标配。

So I think most of you listening to this are familiar with the tidyverse and dplyr in particular, where you get your full gamut of being able to create new variables, filtering, do other transformations, it's become a big staple in the world of data science and data munging in particular.

Speaker 0

但你要是直接用默认的dplyr跑一连串mutate和filter操作,通常不会看到任何输出,除非你特意去调取最终生成的那个数据框。

Well, when you run dplyr off the shelf and you're doing a batch of mutate filter steps, you typically won't see any output unless you ask for that resulting data frame that you get back out of that pipeline.

Speaker 0

这其实是有意设计的,对吧?

That's typically by design, right?

Speaker 0

大多数管道默认都是静默的,除非你主动要求更详细的输出。

Most pipelines will, you know, only let you opt in to more verbosity if you want to.

Speaker 0

但在其他语言里,比如我行业里还很主流的SAS,它的PROC DATA步骤会在日志里打印一大堆信息——比如处理了多少条记录、删了多少、加了多少,啥都有。

Well, in the world of other languages, especially one that is still fairly dominant in my industry, such as SAS, you do get in its PROC DATA step actually a pretty copious amount of messages in SAS's log file when the data processing is being conducted, such as a number of observations and maybe some records that were deleted or added and whatnot.

Speaker 0

不过到目前为止,dplyr或者tidyverse里还没人搞出个类似的东西。

But up until now there wasn't really an equivalent version of that in the world of dplyr or tidyverse pipelines.

Speaker 0

今天第一个亮点,我们要介绍一个叫logrittr的新R包。

Well, in our first highlight today, we are going to talk about a new R package called logrittr.

Speaker 0

这名字听着跟magrittr很像,我猜发音也差不多,它是个专为记录dplyr管道操作而设计的‘verbose管道’,作者是Guillaume Pressiat,应该是法国人吧,有错欢迎纠正——他搞了这个叫logrittr的包,功能很简单:给你一个新操作符,能直接替换掉你原来的magrittr管道或者R原生的管道。

I'm going to take a venture that's the way to pronounce it given its similarity to the magrittr, which is tagged as a verbose pipe operator for logging dplyr pipelines and this package and accompanying blog post is offered by Guillaume Pressiat, I believe that is French correct me if I'm wrong out there, but he has created, yep, like I said, this aforementioned package called logrittr, and on the tin what it gives you is a different operator that you can use to replace your call to either the Magrittr pipe or even the base r pipe, and the operator for this is surrounded by a percent sign with a greater than sign and an equal sign immediately afterwards.

Speaker 0

就像magrittr管道是%>%一样,这个新操作符就是%>=%,就是在大于号后面加个等号。

So much like the Magrittr pipeline, which I believe also had its origins from France, but you just add an equal sign after that greater than sign in between the percent signs.

Speaker 0

博客里有个例子,用iris数据集做了一轮筛选和变换,运行时终端会实时弹出信息:每一步操作后,都会显示当前数据有多少行、多少列、花了多久,还会标出跟上一步相比,行数或列数增加了还是减少了多少。

So in the example on the blog post, the author does a simple filtering and mutating step with the iris data set, and what you see is the output is showing kind of these interesting messages in the terminal that at each step of the pipeline, whether it's just converting it to a tibble, doing the filtering, doing the mutating, it's going to indicate the number of rows, the number of columns, the time it took to complete it, but as far as kind of like a diff is concerned, it will show next to the appropriate rows or column numbers how many were either added or subtracted at that particular step.

Speaker 0

比如,它筛选出‘花萼长度小于5’的数据时,从原来的150条一下子降到22条,旁边就会直接标个‘-128’,一目了然。

So for instance, when it does the filter for the sepal length being less than five, it goes from 150 observations in the iris data set down to 22, and then it has next to that 22 the delta of that difference, minus 128.

Speaker 0

所以如果你在做审计时想快速了解数据行数和变量数的变化,这个功能就特别有用。

So this could be very helpful if you want a quick read in an audit, so to speak, of what's happening in terms of the number of observations and the number of variables that are being added or subtracted.

Speaker 0

这个功能非常实用,文章后面还提到,这个包和另一个可能去年我们提过的包——Lumberjack——配合得非常好,那个包对跟踪dplyr流水线中的数据变化有自己独特的日志方式。

So this one is again very utilitarian, the rest of the post does mention that the package does play nicely with another package that we may or may not have talked about maybe last year called Lumberjack, which kind of has its own unique take on messaging and logging with respect to tracking changes in your data with typical dplyr like pipelines.

Speaker 0

如果你打算把这两个工具结合起来用,那确实挺有意思的,不过这个思路本身已经很新颖了。

So that might be interesting if you want to do a nice combo of those together, but this is a very interesting take on this.

Speaker 0

文章最后也提到了一些限制:目前这个包只支持R会话里的数据框,对通过dplyr连接的数据库就不太适用了,比如你用dplyr当前端去查数据库的情况。

The post concludes with some limitations in that this is really only going to be supportive for data frames themselves in your R session, not so much with databases themselves that you might interface with the dplyr package as your front end to those kind of queries and whatnot.

Speaker 0

还有,连接时的基数统计目前处理起来有点棘手,不过听说作者已经在着手改进了,他正在弄这个。

And there, and it sounds like join cardinalities are a little bit difficult to handle, but it looks like he is working on that, he is working on that.

Speaker 0

但总的来说,作者鼓励大家去试试看,节目笔记里会放上包的文档链接,如果你只是想稍微窥一眼你的dplyr流水线到底对数据做了什么,这工具还真挺管用的。

But yeah, he invites others to give it a test and there will be a link to the package documentation in the show notes, but it might be a useful tool if you want just a little bit of a glimpse into what your dplyr pipeline's doing to your precious data.

Speaker 1

这真是个超酷的新包。

This is a really cool, new new package out there.

Speaker 1

而且就像Guillaume自己在博客里说的,他其实是在探索CLI包能玩出什么新花样,把边界往前提了提。

And I think as as Guillaume says himself in this blog post, it's really kind of pushing the boundaries or he was really exploring what you can do with CLI, the CLI package.

Speaker 1

你看这个控制台输出,顶部有个标题,下面信息图表和数据排得整整齐齐,特别漂亮。

And if you see kind of this this beautiful console printing, with this header at the top and then sort of information graphics and and things laid out in very structured rows neatly in the console, it's it's fantastic.

Speaker 1

我的意思是,这些信息呈现得真叫一个清爽。

I mean, the information is just presented there so nicely.

Speaker 1

我觉得过去几年,这种美观的命令行界面越来越多了,不只是那个CLIR包,你懂的。

And I think we've seen probably, you know, in the last couple of years a lot of movement to kind of beautiful CLIs, not just the CLIR package, if you will.

Speaker 1

这个包也是这方面的一个绝佳例子。

And I think this is a a great example of that as well.

Speaker 1

吉约姆还提到了另一个相关的包,叫tidylog,他说正是这个包给了他开发logrittr的灵感。

One other adjacent package that Guillaume mentions is the tidylog package, which he said gave him some motivation to develop the logrittr package.

Speaker 1

但tidylog的做法不太一样,它是通过覆盖dplyr的函数来工作的,而不是跟dplyr并行协作。

But tidylog, in a different way, kind of works by masking dplyr functions, instead of really working alongside dplyr instead.

Speaker 1

所以,埃里克,跟你一样,我对SAS的proc log输出太熟悉了,每次得盯着SAS脚本,还得看配套的日志文件。

So I, like you, Eric, are probably a little too familiar with SAS proc log, outputs and having to take a look at, not only like a, you know, a SAS script, but also sort of the log script that goes alongside that.

Speaker 1

说实话,能清楚看到这些日志,追踪数据在后台发生了什么,真的挺舒服的——毕竟在R里干活,我们通常都看不到这些,除非你特意加了print语句、日志或者更新提示什么的。

And I'll be honest, it is nice, to be able to kind of see those those logs and help keep track of of what's happening to the data behind the scenes, in a way that, you know, is often abstracted from us if you are, you know, working in R and not baking in a lot of CLI or print statements or logging or updates, things like that.

Speaker 1

所以我觉得,这个 logrittr 包正好填补了这个空白。

So this is a really, I think, way with this logrittr package to be able to kind of fill that gap.

Speaker 0

是的。

Yeah.

Speaker 0

特别在生命科学领域,SAS 的日志模式影响深远,现在 R 里都有不少包在努力复刻这些功能。

And it's amazing how the SAS logging paradigm has had such influence in, especially in life sciences, that there are packages in R to try and replicate a lot of that functionality.

Speaker 0

这确实解决了我们的一大痛点。

This is certainly scratching a bit of that itch.

Speaker 0

就连 pharmaverse 里也有像 logrx 这样的包,就是为了让 R 用户能轻松生成日志,方便以后做合规审计。

Even in the pharmaverse, are certain packages like logrx and the like that are trying to make it so that it's easy as an r user to have that log ready to go in case you need to, you know, audit it for more regulatory purposes.

Speaker 0

不过话说回来,这东西真不错,我觉得它是个完美的中间方案——用起来特别简单,就差一个管道符而已。

But yes, it's good to have because I'm going to call it like an in between here because it's so easy to opt into this and it's just one pipe operator different.

Speaker 0

你原来的 dplyr 代码完全不用改,也不用在脚本开头设什么隐藏参数。

Everything else in your dplyr code can remain the same, there's no like hidden options you have to specify at the top of your script.

Speaker 0

这真是个特别优雅的解决方案,我很期待这个包以后还会有什么新发展。

This is a really elegant solution and I'm eager to see, you know, maybe what future development is in this great package.

Speaker 0

今天我们下一个亮点,简直像重回课堂,重温一个看似简单到容易被忽略的概念——它表面平平无奇,但背后藏着大量你根本没注意到、甚至压根没想到去深究的东西,直到这周的这篇亮点文章突然让我们眼前一亮。

And our next highlight today is very much feeling like we're going back to school a bit on a concept that is easy, and I do mean really easy, to take for granted, but has so much going on under the hood that I did not realize or maybe I just didn't think to look until this great highlight that came to our eyes, so to speak, in this week's issue.

Speaker 0

这篇亮点的作者和标题是《用一个种子,在宇宙中生成无数宇宙》。

And the author and the title of this highlight is Generating Universes Within Universes With a Single Seed.

Speaker 0

光是这个标题就信息量爆炸了,我们接下来尽量把它的意思掰开揉碎讲清楚。

There's a lot to unpack even in that message alone or that tagline and we'll unpack that here as best we can.

Speaker 0

这篇文章的作者是备受尊敬的安德鲁·海斯,他可是我们每周亮点的常客,每次发文章都特别有意思。

And the author of this post is the esteemed Andrew Heiss, very frequent contributor to our weekly highlights, always an entertaining read whenever he posts out there.

Speaker 0

他做教育内容和线上分享,用的全是当下最潮的技术栈,这篇文里也藏着不少酷炫的新东西。

And yes, he is such a modern stack with what he's building in his educational materials and his online presence, and there's some cool modern stuff in this post here.

Speaker 0

咱们还是先切入正题吧。

But let's kind of dive into this a bit.

Speaker 0

我和迈克会分工合作,各自讲讲这篇文章的不同部分。

Mike and I will tag team on different parts of this post here.

Speaker 0

说到底,种子这个东西特别关键——作为统计学家,我深有体会,它能确保你分析里所有带随机性的环节都能复现。

But in essence, seeds are a critical part, and this is speaking from my experience as well as a statistician, to ensure reproducibility in whatever random ish kind of process your analyses are employing.

Speaker 0

这一点尤其重要,当我们讨论贝叶斯方法或者数据模拟这类算法时,我们得确保能设一个固定的标识符,这样哪怕在另一个 R 会话里重复运行,结果也能保持一致。

And this is especially true as we look at algorithms from say Bayesian methodologies and even just simulation of data itself, we want to make sure that we can have that one ID that we can assign so that if we run this over and over again in a different R session, we will get that similar result.

Speaker 0

Andrew 和我一样,用种子参数已经很久了。其实这篇文章开头就提到了一个大家可能想不到的地方——ggplot2 里处理数据重叠时也会用到种子。比如用 Palmer 企鹅数据集,如果你默认用 geom_point 画图,按物种分面,X 轴是体长,Y 轴是鳍肢长度,结果屏幕上就冒出五六个小点,但其实每个点背后都藏着一堆数据。你想看清它们的分布,就得加点抖动。

Now, but, and then Andrew, of course, has been using seeds for as long as I have in R, and in fact the post starts with an area they might not expect seeds to play in, and that's with ggplot2 when you want to tackle over plotting, in the case with the Palmer Penguin data set, where if you just do the geom point by default when you look at the facets of species on the X and I1 on the Y, you just get basically six or like five dots on the plot, but those dots have a lot going on because they're the same, but if you want to distinguish them, you want to introduce a jitter.

Speaker 0

这么一抖,所有数据点就散开了,不再是那几个重叠的点,而是完整呈现了全部数据。

So then you get that kind of scattered representation of all the data points instead of seeing just five data points, you now get the full set of data points.

Speaker 0

但问题是,每次你重新运行这段抖动代码,图案都会不一样,因为抖动本身是个随机过程。

But if you run that jitter code again, the pattern will look different because that jitter is actually a random ish process for how it's jittering all the points.

Speaker 0

所以要想每次图案都一样,你就得设个种子。在 R 里这事儿特别简单,就一行代码:set.seed(),后面跟个数字就行。

So if you want that pattern to be consistent, you got to set yourself a seed there and that is, again, how to do this in R, it's a one liner folks, it is just set dot seed and then you give it a number.

Speaker 0

数字该选多大我们待会儿再说,但在这之前,文章还讲了个挺有意思的事儿:种子不光在统计编程里有用,像《我的世界》这种游戏,生成世界时也靠种子。你不设种子,每次新开游戏地图都随机;但只要你设了种子,它就会用同一个种子生成一模一样的世界。

We'll get to how big that number is in a little bit, but even before that point, the post has an entertaining diversion that seeds are also used in many different places outside of statistical programming, such as Minecraft, when it generates that world that you are spawned into, unless you tell it to with a seed, it will always be random each time, but you could set a seed and it will use that in its procedural generation of the different worlds out there.

Speaker 0

很多《我的世界》玩家都深陷这个坑,专门去挖各种神奇种子,看看能生成出什么稀奇古怪的地图。

And there has been a whole like rabbit hole that many in the Minecraft community have gone down to try and kind of see interesting things that happen with specific seeds.

Speaker 0

对,真有个专门讨论这个的 Reddit 子版块。

So yes, there is a sub reddit just for that alone.

Speaker 0

这周末干这个可真够耗时间的。

That would be an interesting time sink on the weekend.

Speaker 0

不过咱们还是回到种子本身吧。

But back to seeds themselves.

Speaker 0

安德鲁对这事儿有个大致想法,但咱们先退一步,到底啥叫种子?从大方向上说。

So Andrew's had kind of a notion of how he thought this would work, but let's first kind of take a step back here, and what is a seed exactly in the big picture here?

Speaker 0

他提到的定义是:种子就是一个任意的整数,R的内部随机数生成器(简称RNG)得靠它来启动整个过程。

Well, the definition that he highlights here is that it's an arbitrary integer that R's internal random number generator or RNG for short, whatever that algorithm needs to initialize that process.

Speaker 0

这个关键点我们待会儿还会再聊,其实你可以用无数个数字,像我这把老骨头,最爱用的种子就是8675309。

That will be a key we also get back to later on, And you could use, as I said, many, many different numbers for this, even to my 80s or 90s heart, puts in my favorite seed of 8675309.

Speaker 1

太棒了。

Love it.

Speaker 0

我可不会靠这个改行,这真是我的心头好,说实话,真拿它做过项目。

I will not quit my day job, it's my favorite one and I won't lie, have used this for actual projects.

Speaker 0

我的天哪。

Goodness gracious.

Speaker 0

好了,不管怎样,咱们接着说。

Okay, anyway, so.

Speaker 0

珍妮,珍妮。

Jenny, Jenny.

Speaker 0

对,珍妮,亲爱的珍妮。

Yep, Jenny, Jenny baby.

Speaker 0

在他在接下来几篇文章里,他提到自己是怎么给学生解释种子这个概念的。有个挺有意思的练习是:当你在 R 里用 runif 函数——就是 r u n I f ——这个函数能生成均匀分布的随机数,这在统计学里很常见。如果你连续跑好几次,比如要五个数,每次结果都不一样;但只要你设了种子,第一次跑的时候会得到一组固定的五个数。然后他让班上其他同学也都用同一个种子、跑同一行代码,结果大家第一次跑出来的数都一模一样。

So in his and the rest of the posts in the next section he talks about what kind of he's been telling his students based on his mental model of how seeds work and one interesting exercise is that when you use the runif function or r u n I f in R, this is the function that'll let you generate random numbers from the random uniform distribution, another staple in statistical theory, and of course if you run this with say five to get five numbers repeatedly, they'll be different every time, but then when you set the seed and you run that, the first time you will get a set of five numbers and then he had the other students in his class set that same seed, run that same line, and they all get the same number that first time they run it.

Speaker 0

有意思的是,如果你再跑一遍 runif(5),出来的五个数就又不一样了。

It's interesting that if you run that statement again, the run you if of five, you get a different set of five numbers.

Speaker 0

其实这背后的原因是,这些随机数生成器本质上是在输出一串预先算好的数字序列,你每次调用函数,就相当于从这串序列里取走几个数,就像从栈顶弹出一样。

And what's interesting about this is that these random number generators are in essence giving you a sequence of these numbers, and you just kind of, in essence, push them or pop them off a stack every time you call this different invocation with that same length.

Speaker 0

那到底是什么在驱动这个过程呢?

And so what is actually driving process?

Speaker 0

这可不是什么魔法,它背后有严格的数学理论支撑,可问题是,这一长串数字到底从哪儿来的?

It's not hocus pocus, this is based on mathematical theory, just where does this grand list of numbers come from?

Speaker 0

所以有个挺经典的算法叫线性同余生成器,简称LCG,它有一组关键参数来决定这个序列,他在博客里用了一个很清晰的公式来表示,关键参数包括乘数、增量和模运算,这些都能帮你根据上一个数算出下一个数。

So there is a relatively classic algorithm called linear congruential generator, or LCG for short, that actually has a set of key parameters in this sequence, and he has a nice notation of the equation in the blog post here, but some of the key inputs include things like the multiplier, the increment, and the modulus operator that in essence help you determine what the next number will be based on what was the previous number.

Speaker 0

所以有意思的是,你得先有个起点,因为每个数都依赖前一个数,而这个起点——你猜对了——就是你一开始设的那个种子,它才是整个序列的开端。

And hence, what's interesting is that you need something to start with because it's based on what was done previously, and that what you start with is, you guessed it right, is that seed that you define up front that kicks off the entire sequence.

Speaker 0

这篇博文最酷的地方是,Andrew给了你一个互动工具,你可以用下载的Excel表格,或者网页里嵌入的一个JavaScript小工具,直接输数字、拖滑块,实时看到结果怎么变。

And what's really cool about this post is that Andrew has a way for you to interactively play with these different inputs to this lcg algorithm in both a downloadable excel spreadsheet xlsx file as well as an embedded looks like a javascript widget that you can literally put in the numbers and it will update it on the fly with the sliders you can toggle.

Speaker 0

我现在就正在玩,你能清楚看到,换不同的参数,接下来生成的数字序列会怎么变化。

I'm doing it right now and you kind of see what the next numbers will be in the different sequence depending on the numbers you decide to generate.

Speaker 0

你可以从12开始,往上设到多高都行。

You could go anywhere from 12 up to much higher too.

Speaker 0

这样动手玩一玩,特别有意思。

This would be a fun way to play with this a bit.

Speaker 0

他在研究这个算法时发现了一个关键点:最终你一定会遇到循环,数字开始重复。

And here is the nugget that he's found in researching this algorithm, is that eventually you're going to reach a point where there is a cycle of numbers and they are repeated.

Speaker 0

这让我愣了一下,原来这序列里的数字并不是全都独一无二的。

That kind of took me a step back a bit, that these are not all unique numbers in this sequence.

Speaker 0

确实有个时候,这其实是LCG算法的一个局限——你得根据输入的参数,去应对这些不同的周期或循环。

There is a time, and this is actually a limitation of the OCG algorithm, where you will have to you know account for these different periods or cycles based on those inputs that you put into the equation.

Speaker 0

听到这个局限,你可能要问了:哎呀,那我们在R里做随机数生成的时候,是不是要出问题了?

So hearing this limitation you may be wondering, uh-oh are we in trouble here when we do random number generation in r?

Speaker 0

别担心,R本身默认根本不用LCG算法。

No, threat not, because r itself does not use the lcg algorithm as its default.

Speaker 0

它用的是更复杂的方法,目前R默认的算法叫梅森旋转算法(Mersenne Twister)。

It actually uses a more complex technique and then the default for R at the time of this recording is called the Mersen twister algorithm.

Speaker 0

这个算法特别有意思,博客里有链接可以深入了解,简单说就是,你得生成海量数字才会碰到重复的周期,你可能要问:到底得多长?

This is a very interesting one, there are links in the blog posts that talk about this in more detail, but in essence you can be assured that it will take a long time for you to encounter this different cycle to repeat itself, and you might say how long will it take?

Speaker 0

大概要到2的19937次方减1这么大的数,你不用我告诉你,这数字大得离谱。

Well, it takes about two to the power of 19,937 one, which you don't need me to tell you that's a really big number.

Speaker 0

所以在你撞上这个数之前,你的随机数生成器根本不会出现重复序列。

So until you hit that number you're not going to see this repeated sequence of numbers in your random number generator.

Speaker 0

他还特别推荐了Danielle Navarro的一篇好文,她2023年写了一篇叫《SeedCatcher》的博客,专门深入剖析了这个梅森旋转算法。

He also shouts out a great post from Danielle Navarro, when she looked at in 2023 a blog post, that she authored, called seedcatcher to try and dive into more of this, mersane twister algorithm.

Speaker 0

这篇也挺值得一读的。

It's a cool read as well.

Speaker 0

单就这一点来说,已经是个不错的开端了,对吧?

Now in and of itself this is a great start, right?

Speaker 0

但你可能会遇到一些有意思的情况,圈子里一些大牛对这事儿也有不少激烈的看法——比如著名的Claws Wilk就说过:如果你的随机种子设成42,我就上门把你电脑点着了,这跟Jenny Bryan在脚本顶部写‘workspace’那句如出一辙。

But there are some interesting things that you can encounter and in essence there are some hot takes from very esteemed members of the community on this, and in fact the esteemed claws wilk has a great take of, if your random seed is 42, I will come to your office and set your computer on fire shades of Jenny Bryan there with the workspace line in the top of our scripts.

Speaker 0

这正好引出了我们接下来要聊的话题:使用随机数生成器时,有哪些坑是你得特别留意的?

And that is just kind of a dovetail into what are some of the pitfalls that might happen based on random number generator that you might want to watch out for.

Speaker 0

所以Mike,你来给我们讲讲,眼下大家对这事儿都有哪些热门观点?

So Mike, why don't you take us through kind of overtakes on this, hot topic that we're seeing here?

Speaker 1

是啊,我觉得Andrew想表达的核心是,所谓的‘随机’其实并不真随机,因为你设的种子决定了后面一连串数字的走向——这当然对复现结果特别有帮助,但对其他场景可能反而成问题。

Yeah, I mean I think the core idea that Andrew's trying to present here is that you know random isn't really random, because your seed is going to kind of create this fixed predetermined downstream sequence of numbers, which is great, right, for reproducibility, but also can hurt for other applications.

Speaker 1

你其实是在按一个预设的列表走,而不是实时生成真正的随机数。

You're of walking through a prewritten list and not generating complete randomness on the fly.

Speaker 1

所以用种子,就像是选了一个宇宙,在这个宇宙里,所有随机事件早就被定好了。

So, you know, kind of using a a seed is is like choosing a a universe where every random event is already sort of decided.

Speaker 1

当你用那些常见的种子,比如42、1、2、3、4的时候,其实你和别人都在同一个‘宇宙’里打转,没去探索别的可能性。

And when obviously you use those common seeds, 42, one, two, three, four, it means that you and everyone else are kind of exploring just that same, select universe and not trying other universes, if you will.

Speaker 1

对。

Will.

Speaker 1

你只是在重复同一串数字而已。

You're just exploring that same sequence of numbers.

Speaker 1

这样一来,我们的分析就变得人为地局限了。

So it creates sort of artificially limited variation in in our analysis.

Speaker 1

理论上存在无限多个宇宙,可大家偏偏都挑同一个种子,结果全挤在一个宇宙里。

There's infinite universes, everyone keeps picking the same one if you're all choosing the same seeds.

Speaker 1

还有一点值得考虑,就是‘种子作弊’的可能性。

You know, another thing to think about here is the possibility of of seed hacking.

Speaker 1

你可以专门去找一个能出你想要结果的种子。

So you can search for a seed that gives the result that you want.

Speaker 1

我要是说以前从来没为了把图做得好看点、好说明我的观点,而换过几次种子,那我就是在骗你。

And I would be lying if I said that in the past, I haven't changed my seed or, you know, a few different times to try to make my plot look a little bit nicer, to explain the thing that I'm the point that I'm trying to get across.

Speaker 1

我得说,我从来没干过这事儿——毕竟我觉着自己睡不着觉,怎么可能为了把系数从负变正就随便换种子呢?

I will say that I have never, because I don't think I could sleep over this, you know, just changed my seed to like change a coefficient from negative to positive Mhmm.

Speaker 1

或者类似这种操作。

Or something like that.

Speaker 1

但你完全可以想象,这种事确实可能发生。在那篇博文里,我提到过 Nantz Yew 和 Podnard 这两个人。

But you could think that that's certainly possible And there is an area in the in the blog post, where I think that there were, Nantz Yew and and Podnard.

Speaker 1

我不确定他们叫啥名,但引用里的链接在这儿呢。

I'm not sure what their their first name is, but, you know, the the link in the citation is here.

Speaker 1

在那篇博文里,他们用了好几种机器学习方法,比如随机森林、神经网络,这些方法本身都带点随机性。

In the blog post, they used sort of several different machine learning techniques, random forests, neural nets, which all, you know, incorporate some element of randomness.

Speaker 1

他们分析的是同一组数据,估算的也是同一个平均处理效应,但结果却因为选的种子不同而差了老远。

They analyzed the same data, and estimated sort of the same average treatment effect, and estimated effect varied substantially based upon the seed that they chose.

Speaker 1

这就让事情变得很棘手了,很明显,这给‘种子作弊’留了很大空间——不只是作弊,更可怕的是,你只看到自己那个‘宇宙’里的东西,根本没把足够多的变量考虑进去,看不到其他可能性。

So, you know, that makes that makes it tricky and obviously leads to a lot of potential for seed hacking or, you know, not only seed hacking, but, you know, just only seeing things in your individual universe and not really baking in enough variation to sort of take a look at all of the different possibilities.

Speaker 1

我觉得,当你往信息安全这类领域延伸,超出了数据科学的范围,这种问题就开始有真实后果了——有人能拿这个搞欺诈,手段可多了。

And I think when you get into like info sec and things like that beyond data science, that's where you start to have some real world consequences of this because people can exploit, this in ways, you know, that are fraudulent.

Speaker 1

安德鲁接着引出了下一个话题,讲的是怎么绕过这个问题——毕竟结论是,计算机本身根本没法真正产生随机性。

And Andrew makes sort of the leads into sort of a next section about how you can potentially get around this because, you know, the conclusion here is that computers themselves just can't generate true randomness on their own.

Speaker 1

只要你懂它用的算法,又知道那个初始种子,就能预测出所有后续的数值。

So if you you kind of know the algorithm that's being used and you know that seed, that starting point, you can predict all future values.

Speaker 1

所以,纯算法生成的随机数,本质上其实是确定性的。

So pure algorithmic randomness is is kind of deterministic under the hood.

Speaker 1

安德鲁认为,要造出真正的随机数,得靠一些外部的物理过程,比如现实世界里的熵。

And Andrew's argument for creating real randomness is that you need some sort of external, like, physical process or physical entropy in the world.

Speaker 1

他还举了好几个特别棒的例子。

And he has some great examples of this.

Speaker 1

我特别喜欢的一个是Cloudflare。

One of my favorite here is Cloudflare.

Speaker 1

埃里克,你有没有看过这个?有。

I don't know if you saw this Yes.

Speaker 1

他们是全球最大的内容分发网络之一,处理海量加密任务,比如SSL加密、HTTPS这类东西。

Eric, but they're one of the largest, like, content delivery networks, CDNs on the the Internet, and they handle a ton of, like, encryption related tasks, like SSL encryption, you know, h think HTTPS type of, stuff.

Speaker 1

他们在旧金山的办公室里摆了一面墙,大概有一百个泡泡灯之类的东西。

And they have this wall in their office of, I don't know, a 100 lava lamps, something like that in their San Francisco office.

Speaker 1

他们装了个摄像头,专门对着这面泡泡灯墙,拍下液体流动的状态,把图片里的像素转化成数字,再用这个来自真实世界的泡泡灯图像,生成一个真正的随机数种子——简直太疯狂了。

And they have a camera that looks that just watches these, this wall of lava lamps takes a picture of, you know, the, the state of the fluid moving around in these lava lamps, assigns a numeric value to the pixels in that picture, and then generates, you know, a seed or a truly random, sort of process downstream based upon that physical picture of the lava lamps, which is a wild thing.

Speaker 1

还有几个别的例子,比如一个叫 PGP 的服务。

Know, there's a couple other examples here where, there's a service called PGP.

Speaker 1

我记得全称是 Pretty Good Privacy,就是‘相当不错的隐私’。

I think it stands for pretty good privacy.

Speaker 1

对。

Right.

Speaker 1

它是个命令行工具,让你在屏幕上乱动鼠标,再随便敲一串键盘输入。

It's like a terminal interface where it lets you move your mouse around and kind of type random stuff into your keyboard.

Speaker 1

然后它会根据你的鼠标轨迹和敲的那些乱七八糟的按键,给你生成一个真正随机的伪随机种子。

And based upon your mouse movements and whatever you typed, it will create a, you know, truly kind of random, pseudo random seed for you.

Speaker 1

还有一个例子,是用大气噪音,有个叫 random.org 的公司,装了三台收音机,专门捕捉大气中的杂音,再用这些噪音的随机性来生成真正的随机数。

And there's another example, using atmospheric noise is a company called random.org that has three radios that capture atmospheric noise and then uses the entropy of that noise to generate truly random numbers.

Speaker 1

安德鲁就用过这个方法。

And that's something that Andrew shows.

Speaker 1

他其实用得挺多的。

He actually uses quite a bit.

Speaker 1

他经常会去 random.org 输入一串随机数,然后拿它输出的结果当种子。

He'll go to random.org to, first, you know, type some random number into random.org, and then whatever that spits out, he'll use for his seed.

Speaker 1

我觉得这个做法也挺有意思的。

So I thought that that was pretty interesting there as well.

Speaker 1

说到底,关键在于,只要你一设好种子,接下来每一次抛硬币、每次洗牌、每次模拟结果,其实早就定好了。

And, I you know, again, I think sort of the the big philosophical takeaway here is, you know, every coin flip, every every shuffle or or simulation outcome is is already gonna be predetermined, the moment that you set your seed.

Speaker 1

所以咱们在不同项目里选种子的时候,一定要尽量用多样化的来源。

So I think it's important to make sure that you're leveraging a lot of variation in the seeds that you choose from project to project.

Speaker 0

对。

Yeah.

Speaker 0

特别有意思的是,不光咱们写代码时用到这些,其他行业也在用各种冷门方法,给随机性找一个容易被忽略的起点。

It's really interesting to see not just the parts that are happening in our actual programming work, but then how these other industries are tapping into these other processes to give it that additional random ish start that is easy to miss.

Speaker 0

虽然我没法像Cloudflare那样买一堆 lava lamp 还拍照片,但Andrew提到的这个点真挺有意思的。

So I, while I won't have the budget to buy all these lava lamps and take pictures like Cloudflare does, that was a really interesting, find that Andrew put in here.

Speaker 0

我对那个 random.org 的整合方式特别感兴趣。

I am intrigued by that random.org kind of integration.

Speaker 0

我觉得我下一个项目里,至少可以照着他那样写个脚本,用来生成种子,挺酷的。

I think that could be really cool in my next project to at least have a script like he has here to get that seed started.

Speaker 0

当然,我平时也会把种子记下来,方便以后复现模拟结果,但按文章说的,也许该跳出我那老掉牙的867539,搞点更靠谱的玩法。

And of course, I document that seed for prosperity in my simulations or whatnot, but that probably will be a nicer thing too, the post mentions, maybe step outside my comfortable universes of 867,539 and do something a little more robust than that.

Speaker 0

这地方真有不少东西可以学。

So there's a lot to learn here.

Speaker 0

一个概念,实现起来简单到不行,可我觉得特别有意思。

I think it's fascinating for a concept that is, from implementation side, as easy as it gets.

Speaker 0

表面上就一行代码,但这一行背后藏着多少门道啊。

There's a one liner, but what goes into that line?

Speaker 0

确实,值得想的挺多的。

Yeah, a lot of things to consider.

Speaker 0

这跟你说的其他数据科学环节其实挺像的,有人会随便搞个种子,这篇帖子就提到了这种现象。

Not to dissimilar to other parts of the data science process that you mentioned, yeah, when people kind of hack seeds, so to speak, that this post highlights.

Speaker 0

当然,这还没到那种反复跑统计检验、非得凑出个0.05显著性结果的p值操纵那么离谱,但后果也差不了多少。

I mean, is maybe not quite as egregious as p value hacking that often happens when you run inferences over and over again until you get to that right result that you want for that lesson 0.05 level, but it can lead to consequences not too dissimilar to that.

Speaker 0

所以长远来看真得好好想想,不过现在作为数据科学家,你其实已经有好几种办法能让种子的起点更自然、更随机一点。

So it is worth thinking about in the long term, but yeah, there are definitely mechanisms you can take as a data scientist right now to give that seed a more gentle or more random ish start.

Speaker 0

这内容真太棒了。

So really cool stuff here.

Speaker 1

我见过最深入讲种子的分析了。

The deepest dive on seeds I've ever seen.

Speaker 0

对啊。

Yeah.

Speaker 0

如果你之前还觉得用不用种子无所谓,听完这个,你肯定改变主意了。

And, if you didn't feel the need for seeding, well, you sure do now after that.

Speaker 0

好了,今天咱们的亮点就先说到这儿。

But and then, rounding out our highlights today.

Speaker 0

最后一个有点简略了,因为我特意去搜了这段演讲的录音,但没找到。

They're actually gonna be a bit abbreviated with this last one because yours truly did some sleuthing and tried to find the recording of the talk that this mentions, and I could not find it.

Speaker 0

不过得给妮可·奥鲁尼点个赞,她也是我们Highlights的常驻贡献者之一。在R-Ladies远程小组里,她和其他人一起参加了一场挺有意思的圆桌讨论,主题叫‘更聪明地用R:技巧、窍门与实战经验’。

But we'll give a quick shout out here to Nicole O'Runie, another frequent contributor to the Highlights, where she amongst others in the rladies remote group were part of a, sounds like a really interesting panel discussion, that was tagged working smarter for r tips tricks and real world lessons.

Speaker 0

虽然没看到原演讲,没法给你做个精炼版的总结,但我可以告诉你,节目笔记里附的链接里有很多额外资源——虽然我也没全看完,但里面有不少值得一看的东西,比如R Markdown、Quarto、妮可最近在做的可视化项目,还有我们一直特别偏爱的targets包,等等。

So without access to the talk, I can't give you like my Reader's Digest version, so to speak, on the points I highlighted, but what I can say is that the link we have in the show notes has a copious amount of additional links that admittedly I don't have the full context around, but there are some interesting things you might look at, as you're waiting for the talk recording to get out there, covering the full gamut of R Markdown, Quarto, some of Nicola's recent work on visualization, and yes, even the targets package, we're always a soft spot for that amongst others.

Speaker 0

我们每周都会整理一堆额外链接,这正是我们的专长,说不定能帮你找到更聪明地用R的切入点。

So definitely additional links within our weekly, that's kind of what we're specialized in, that might give you a starting point to working smarter with R.

Speaker 0

好了,这期的每周精选就先到这里。

And with that we're gonna put a bow on this episode of our weekly highlights.

Speaker 0

时间有点紧,我们得赶紧收尾,没空再聊别的发现,不过老规矩,欢迎你去翻翻往期内容,慢慢看,里面好东西不少。

We got to get out of here kind of quick so we don't have time for additional finds, but as always we invite you to check out the issue and yeah read at your leisure for all the great content that's shared on this end in the back catalog as well.

Speaker 0

当然,这个项目全靠社区支撑,这点我待会在positconf的闪电演讲里也会重点提——最简单的参与方式就是分享你发现的好资源:一篇好博文、一个新包,或者任何精彩发现,我们只差一个pull request。

And of course this project is built on the community, which will be a very key point that I highlight in my upcoming lightning talk at positconf about our weekly and one of the best ways to contribute is to share that great new resource that blog post about a package or another great find We're just a pull request away folks.

Speaker 0

如果你熟悉markdown和GitHub,那上手绝对没压力。

If you're comfortable with markdown and github, you'll be right at home with this.

Speaker 0

在 rweekly.org 页面的右上角就有个链接,点进去就能直接提交 PR,模板都给你准备好了。

A link is right at the top right corner of the rweekly.org page for you to send that pull request with the template already ready for you to complete.

Speaker 0

另外你也可以在社交媒体上找到我们,说不定还惹点小麻烦,哈哈——对了,我主要用哪三个平台来着?

And also you can find us on the social medias causing you know some amount of trouble there or not, but I am also well what are my big three?

Speaker 0

这事儿我都该背下来了。

I should have it memorized by now.

Speaker 0

我在 Bluesky 上,账号是 rpodcastbsky.social。

I am on Bluesky with rpodcastbsky.social.

Speaker 0

我还在 Mastodon 上,用户名是 rpodcastpodcastindex.social;LinkedIn 上也能搜到我,我会在那儿发些重要通知和最近在捣鼓的有趣东西。

I'm also on Mastodon where I'm rpodcastpodcastindex.social and I'm on LinkedIn, you can search my name and you'll find me there, certain announcements and other fun things I'm working on.

Speaker 0

迈克,听众们上哪儿能找到你?

Mike, where can the listeners find you?

Speaker 1

我在 Bluesky 上的账号是 Mike-dash-Thomas.bsky.social。另外特别感谢 John Harmon,我们俩两周没露面,他一听说我们回来了,激动得不行。

You can find me on Bluesky at Mike dash Thomas dot b s k y dot social, and a big shout out to John Harmon, who was super excited after our long absence two weeks ago when we came back.

Speaker 1

真的很感谢他在那儿给我们点了个赞,还这么热情地欢迎我们重回播客台。

So really appreciate the shout out from John on there and the excitement about us getting back on the mic.

Speaker 1

上周的事真不怪我们。

Last week was not our fault.

Speaker 1

最近没啥亮点,不过往后我们会尽量更稳定一些。

There were no highlights since, but we hope to, be a little more consistent going forward.

Speaker 1

你也可以在领英上搜‘catch brook analytics’,拼写是 k e t c h b r o o k,看看我最近在忙啥。

And, you can also find me on LinkedIn if you search for catch brook analytics, k e t c h b r o o k, you can see what I'm up to.

Speaker 0

对。

Yes.

Speaker 0

我和迈克正在安排接下来几周的时间表。

Mike and I are just working out our scheduling for the future weeks.

Speaker 0

我觉得咱们找了个挺好的时段。

We think we got a good spot.

Speaker 0

至少我们这边会尽量少出岔子。

We're gonna at least more a problem on yours truly side.

Speaker 0

朋友们,记得保护好你们的日程啊,有时候真是乱得像西部荒野,但懂的都懂。

Protect that calendar folks, man sometimes it just gets to be a wild west out there, but if you know, you know.

Speaker 0

好了,这期的每周亮点就先聊到这里。

Anyway, we're gonna close-up shop here on this episode of our weekly highlights.

Speaker 0

再次感谢大家收听,下周别忘了回来听我们的下一期每周亮点。

We again thank you so much for joining us and we hope to have you listen to next week's episode of our weekly highlights next week.

关于 Bayt 播客

Bayt 提供中文+原文双语音频和字幕,帮助你打破语言障碍,轻松听懂全球优质播客。

继续浏览更多播客