繁体   English   中英

为 R 中的卡方生成 pdf

[英]Generating pdf for chisquared in R

我想要可视化 R 中卡方分布的 pdf。 我对绘图部分没问题,但很难获得数据框。 我想要的是一个自由度为 1、2、3、6 的 100,000 个随机数的 pdf。

这是我尝试过的。

set.seed(1)
data.frame(chisq = 0:100000) 
df1 = dchisq(x = chisq, df = 1)
df2 = dchisq(x = chisq, df = 2)
df3 = dchisq(x = chisq, df = 3)
df6 = dchisq(x = chisq, df = 6)

然后我得到一个错误:

dchisq 中的错误(x = chisq,df = 1):数学 function 的非数字参数

回溯: 1. dchisq(x = chisq, df = 1)

如果您需要 4 个不同的数据帧,则可以避免使用代码的前两行并使用此代码

x = 0:99999   # this way the vector has length 100000 instead of 100001
df1 = data.frame(x = x, dchisq = dchisq(x, df = 1))
df2 = data.frame(x = x, dchisq = dchisq(x, df = 2))
df3 = data.frame(x = x, dchisq = dchisq(x, df = 3))
df6 = data.frame(x = x, dchisq = dchisq(x, df = 6))

编辑

评论中讨论后的代码

ggplot(df1) +
  aes(x = x, y = dchisq) +
  geom_area(fill = "blue")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM