【问题标题】:curve() works in the first example but not the second, but they look identical. Why?curve() 在第一个示例中有效,但在第二个示例中无效,但它们看起来相同。为什么?
【发布时间】:2017-10-20 13:28:24
【问题描述】:

我是 R 新手,尝试做一些练习。我想知道为什么第一个代码可以正常工作,而第二个代码不能。当我尝试运行第二个代码时,它说他找不到函数a

1.)

x = seq(from = - 9, to = 9, len = 100)
curve(dnorm(x,0,2),add = FALSE, from = -9, to = 9)

2.)

x = seq(from = - 9, to = 9, len = 100)
a = dnorm(x,0,2)
curve(a,add = FALSE, from = -9, to = 9)

【问题讨论】:

    标签: r function plot statistics normal-distribution


    【解决方案1】:

    curve 的使用总是令人费解。查看?curve 的第一个参数expr,以及“详细信息”部分。

    事实上,你误解了第一个(有效的)案例。

    curve(dnorm(x, 0, 2), from = 9, to = 9)
    

    不使用您提供的xseq(-9, 9, length = 100),而是选择内部的采样点。请参阅curve 的参数n。在这里,您将 curve 传递给 formal 参数 x 的函数,而不是实值向量,就像在第二个(失败)情况下的 a 一样。

    如果你不相信,我们把线显示改为点显示:

    curve(dnorm(x, 0, 2), n = 10, from = -9, to = 9, type = "p")
    

    你看到了吗?图表上只有10个点,即使你预设了x <- seq(-9, 9, len = 100)

    另一个补充示例是使用xname 进行调整。让我们使用另一个变量,比如foo

    curve(dnorm(foo, 0, 2), from = -9, to = 9, xname = "foo")
    

    在这个 R 会话中没有变量 foo,但上面的代码有效。因为xname 告诉curve foo 是一个形式变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-18
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      相关资源
      最近更新 更多