我正在嘗試在半徑為 1 的磁盤內繪制 N 個樣本的資料,但我不確定如何去做。
在做了一些研究之后,runifdisc似乎是希望獲得結果的函式,但是我無法在不產生錯誤的情況下運行這個函式。
Error in runifdisc(100) : could not find function "runifdisc"
下圖演示了我如何繪制資料。我對 R 很陌生,如果能深入了解我如何解決這個問題,我將不勝感激。

磁盤中的均勻分布
double a = random() * 2 * PI
double r = R * sqrt(random())
// Cartesian coordinates
double x = r * cos(a)
double y = r * sin(a)
uj5u.com熱心網友回復:
實際上沒有必要依賴包來做到這一點。滾動自己的功能非常容易。這里有兩種可能的方法。第一個是粗略的,但也許更容易理解。第二個稍微復雜一些。兩者都基于 tidyverse。
library(tidyverse)
選項 1:在正方形內隨機生成點,然后過濾以僅保留位于圓盤內的那些點。
runifdisc1 <- function(k, n=2000) {
# Crude approach: need to ensure n is sfficiently larger than k to
# ensure at least k points lie within the unit curcle
tibble(x=runif(n, -1, 1), y=runif(n, -1, 1)) %>%
filter(x*x y*y <= 1) %>%
head(k) %>%
ggplot()
geom_point(aes(x=x, y=y))
coord_fixed(ratio=1)
}
runifdisc1(500)

方案二:使用極坐標在單位圓盤內生成點,然后轉換為笛卡爾坐標。
runifdisc2 <- function(k) {
# More sophisticated approach: polar co-ordinates
tibble(theta=runif(k, -pi, pi), r=runif(k), x=r*cos(theta), y=r*sin(theta))%>%
ggplot()
geom_point(aes(x=x, y=y))
coord_fixed(ratio=1)
}
runifdisc2(500)

應該有一種方法可以直接繪制極坐標,但我不知道它是什么。任何人都可以建議嗎?
回答我自己的問題:
runifdisc3 <- function(k) {
tibble(theta=runif(k, -pi, pi), r=runif(k)) %>%
ggplot()
geom_point(aes(x=theta, y=r))
coord_polar("x")
}
runifdisc3(500)

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/466703.html
