【问题标题】:In R, why does example() produce non-examples?在 R 中,为什么 example() 会产生非示例?
【发布时间】:2014-05-02 00:39:01
【问题描述】:

如果我在 R 中输入example(hist), 我得到以下输出:

hist> op <- par(mfrow = c(2, 2))

hist> hist(islands)

Hit <Return> to see next plot: 

输出中的第一行甚至不包含“hist”。那么它是如何使用“hist”的一个例子呢? 也许我不明白这一点,但我只想看到“hist”用法的例子。 请帮我解释一下输出。

【问题讨论】:

  • 您是否尝试过?op 来了解为什么它在那里?第一行用于在一个显示器上显示 4 个图。大多数示例涉及每个示例的几行代码。在这种情况下,第一个示例是直到(包括)par(op) 的所有行,给出了 4 个图表,显示了使用 hist 时不同选项的效果 - 一种理想的方式来查看它的基本用法是。也就是说,他们正在做我希望他们做的事情 - 展示如何使用它以及选项的作用。
  • 这个问题似乎是题外话,因为它不是关于统计数据,而是关于软件设计特性(特别是为什么 R 在如何使用函数的示例中包含的不仅仅是包含特定函数的行)
  • @Glen_b 感谢您解释 ?op 的评论。如果您认为与 R 软件设计相关的问题与此处无关,您对它的相关之处有什么看法吗?
  • 我很高兴 - 确实,热衷 - 如果你能合理地解释它如何属于on topic 的列表,我很高兴在这里接受这个问题。如果做不到这一点,这类问题最明显的地方就是 r-help 邮件列表。您可以在 stackoverflow 或 superuser.SE 上将问题改写为主题,但请仔细检查他们自己的主题列表。另一种可能性是 /r/rstats subreddit。

标签: r output histogram


【解决方案1】:

example(hist) 生成这三个图像:

还有以下文字:

hist> op <- par(mfrow=c(2, 2))

hist> hist(islands)
Waiting to confirm page change...

hist> utils::str(hist(islands, col="gray", labels = TRUE))
List of 7
 $ breaks     : num [1:10] 0 2000 4000 6000 8000 10000 12000 14000 16000 18000
 $ counts     : int [1:9] 41 2 1 1 1 1 0 0 1
 $ intensities: num [1:9] 4.27e-04 2.08e-05 1.04e-05 1.04e-05 1.04e-05 ...
 $ density    : num [1:9] 4.27e-04 2.08e-05 1.04e-05 1.04e-05 1.04e-05 ...
 $ mids       : num [1:9] 1000 3000 5000 7000 9000 11000 13000 15000 17000
 $ xname      : chr "islands"
 $ equidist   : logi TRUE
 - attr(*, "class")= chr "histogram"

hist> hist(sqrt(islands), breaks = 12, col="lightblue", border="pink")

hist> ##-- For non-equidistant breaks, counts should NOT be graphed unscaled:
hist> r <- hist(sqrt(islands), breaks = c(4*0:5, 10*3:5, 70, 100, 140),
hist+           col='blue1')

hist> text(r$mids, r$density, r$counts, adj=c(.5, -.5), col='blue3')

hist> sapply(r[2:3], sum)
     counts intensities 
  48.000000    0.215625 

hist> sum(r$density * diff(r$breaks)) # == 1
[1] 1

hist> lines(r, lty = 3, border = "purple") # -> lines.histogram(*)

hist> par(op)

hist> require(utils) # for str

hist> str(hist(islands, breaks=12, plot= FALSE)) #-> 10 (~= 12) breaks
List of 7
 $ breaks     : num [1:10] 0 2000 4000 6000 8000 10000 12000 14000 16000 18000
 $ counts     : int [1:9] 41 2 1 1 1 1 0 0 1
 $ intensities: num [1:9] 4.27e-04 2.08e-05 1.04e-05 1.04e-05 1.04e-05 ...
 $ density    : num [1:9] 4.27e-04 2.08e-05 1.04e-05 1.04e-05 1.04e-05 ...
 $ mids       : num [1:9] 1000 3000 5000 7000 9000 11000 13000 15000 17000
 $ xname      : chr "islands"
 $ equidist   : logi TRUE
 - attr(*, "class")= chr "histogram"

hist> str(hist(islands, breaks=c(12,20,36,80,200,1000,17000), plot = FALSE))
List of 7
 $ breaks     : num [1:7] 12 20 36 80 200 1000 17000
 $ counts     : int [1:6] 12 11 8 6 4 7
 $ intensities: num [1:6] 0.03125 0.014323 0.003788 0.001042 0.000104 ...
 $ density    : num [1:6] 0.03125 0.014323 0.003788 0.001042 0.000104 ...
 $ mids       : num [1:6] 16 28 58 140 600 9000
 $ xname      : chr "islands"
 $ equidist   : logi FALSE
 - attr(*, "class")= chr "histogram"

hist> hist(islands, breaks=c(12,20,36,80,200,1000,17000), freq = TRUE,
hist+      main = "WRONG histogram") # and warning
Waiting to confirm page change...

hist> require(stats)

hist> set.seed(14)

hist> x <- rchisq(100, df = 4)

hist> ## Don't show: 
hist> op <- par(mfrow = 2:1, mgp = c(1.5, 0.6, 0), mar = .1 + c(3,3:1))

hist> ## End Don't show
hist> ## Comparing data with a model distribution should be done with qqplot()!
hist> qqplot(x, qchisq(ppoints(x), df = 4)); abline(0,1, col = 2, lty = 2)
Waiting to confirm page change...

hist> ## if you really insist on using hist() ... :
hist> hist(x, freq = FALSE, ylim = c(0, 0.2))

hist> curve(dchisq(x, df = 4), col = 2, lty = 2, lwd = 2, add = TRUE)

hist> ## Don't show: 
hist> par(op)

hist> ## End Don't show
hist> 
hist> 
hist>

如果你不点击Enter/Return,你只会得到你发布的内容,这不是完整的例子。点击Enter/Return 会推进情节,这样您就可以按顺序查看每个图像,而不是一次查看所有图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 2013-10-26
    • 2012-09-13
    • 1970-01-01
    • 2017-01-22
    • 2012-03-16
    • 2023-03-26
    相关资源
    最近更新 更多