r - 像普通的 lisp 一样写 R

标签 r common-lisp

<分区>

我最近一直在学习 common lisp,虽然我不是受过训练的程序员,但我真的很喜欢它。我注意到我想因此而以不同的方式编写我的 R 代码。例如,这段代码是对 Verzani Simple R online book 中代码的重新编写。 :

with(list(p=0.5, m=50, n=20, alpha=0.1),
 with(list(phat=rbinom(m,n,p)/n, zstar=1-alpha/2),
      with(list(SE=sqrt((1-phat)*phat/n)),{
        matplot(rbind(phat-SE*zstar,phat+SE*zstar),
                rbind(1:m,1:m), type='l',
                lty=1)
        abline(v=p)})))

这是用 R 编写的一种合理且合理的惯用方式,还是只是一种从另一种语言中拔出方法的例子?我个人喜欢我可以绑定(bind)变量一次,然后不必使用赋值和一般外观。

Verzani书中的代码对比如下(Dason建议修改):

  local({
  m=50;
  n=20;
  p=.5;
  phat=rbinom(m,n,p)/n
  SE = sqrt(phat*(1-phat)/n)
  alpha = 0.10;zstar= qnorm(1-alpha/2)
  matplot(rbind(phat-zstar*SE,phat+zstar*SE), rbind(1:m,1:m),type="l",lty=1)
  abline(v=p)})

编辑:正如 Spacedman 所指出的,这比我的版本更简洁,如果 R 在 lisp 中有类似 LET* 的东西,我的版本会大大改进。如果有人知道我可以简洁地实现这一目标的方法,请发表评论。

最佳答案

你是说:

with(list(p=0.5, m=50, n=20, alpha=0.1),
 with(list(phat=rbinom(m,n,p)/n, zstar=1-alpha/2),
      with(list(SE=sqrt((1-phat)*phat/n)),{
        matplot(rbind(phat-SE*zstar,phat+SE*zstar),
                rbind(1:m,1:m), type='l',
                lty=1)
        abline(v=p)})))

值得做一遍:

local({
  p=0.5;m=50;n=20;alpha=0.1
  phat=rbinom(m,n,p)/n
  zstar=1-alpha/2
  SE=sqrt((1-phat)*phat/n)
  matplot(rbind(phat-SE*zstar,phat+SE*zstar),rbind(1:m,1:m), type='l',lty=1)
  abline(v=p)
})

我休息一下。

关于r - 像普通的 lisp 一样写 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15280711/

相关文章:

r - R中的分钟和秒计算

r - 当列名必须是字符串时如何在data.table中创建新列

lisp - 如何在普通的 lisp 中获取 64 位整数?

common-lisp - 普通口齿不清 : How many argument can a function take?

common-lisp - cffi安装

r - 如何将 ggrepel 与生存图 (ggsurvplot) 结合使用?

javascript - DataTable 中的 Actionbutton 不起作用,Shiny DT

r - ggplot facet_grid 中分面的单独调色板

lisp - SBCL 在 macOS、Linux、FreeBSD 上的路径名通配符约定是什么?

口齿不清错误 : should be lambda expression