在R

时间:2019-10-06 22:19:31

标签: r statistics linear-regression distribution

我正在做this question的c)部分,该部分需要柯西分布。这是我的尝试:

counter <- 0
slopes <- c()

while(counter < 1000) {
  xvals <- runif(n = 100, min = -1, max = 1)
  evals <- rcauchy(100)
  y <- 5 + 3 * xvals + evals
  fit <- lm(y ~ xvals)
  slopes <- c(slopes, fit$coefficients[[2]])
  counter <- sum(counter, 1)
}
mean(slopes)
print(evals)
plot(evals)

但是,我得到了一些非常奇怪的结果,我怀疑它们是否正确:

plot of evals histogram with values of the slopes of 10,000 regression lines first random set with regression line

我还尝试设置评估值<-(x,位置= 0,比例= 1,对数=假),但这不起作用。我只是不了解dcauchy和rcauchy之间的区别,以及如何获取错误以柯西分布。谁能解释这个奇怪的结果?

1 个答案:

答案 0 :(得分:1)

如注释中所述,您的代码正在做“正确的事情”,但是您可能需要调整直觉,以了解尾部发生的事情的重要性

至于功能之间的区别:

遵循此约定的代码有很多相关的集合,rnormdnorm用于正态分布,但是您可以在https://stats.stackexchange.com/q/157662/17060中找到有关此差异的更多注释