[英]Simulating samples from Gamma distribution in R
我在編程作業時遇到了麻煩。
從前面的問題中,我有一個包含 49 個元素的list
。
每個元素都是size=10000
sample
數據。 對於最后一個問題,我必須計算前n
樣本值的平均值。
n
介於1到10000之間。然后我必須為每個數據集繪制這些運行平均值。
我一直在嘗試創建運行平均值的列表/向量,但沒有成功。
有什么我可以做的嗎?
運行平均函數:
run_avg <- function(x, n_max){
a <- c(1:n_max)
r_avg <- sapply(a, FUN = function(y) mean(x[1:y]))
return(r_avg)
}
在您的情況下, n_max 應等於 10000; 然后,此函數為一個數據集創建運行平均值。
然后這必須應用於所有數據集。 如果您的數據集存儲在列表中,您可以為此使用 lapply。 另一種方法可能是循環或類似的東西。
編輯:我看到你的數據集在一個列表中,所以只需使用:
lapply(my_list, run_avg, n_max = 10000)
可以使用以下方法計算運行平均值。
res <- lapply(x, function(y){
sapply(seq_along(y), function(k) mean(y[1:k]))
})
然后,為了使結果列表的格式更適合使用包ggplot2
進行繪圖, ggplot2
格式化為數據框,行名稱作為列。
df_res <- do.call(cbind.data.frame, res)
names(df_res) <- paste("Mean", seq_len(ncol(df_res)), sep = ".")
df_res <- cbind(df_res, id = as.integer(row.names(df_res)))
現在重塑從寬到長和情節。
library(tidyverse)
df_res %>%
pivot_longer(
cols = starts_with("Mean"),
names_to = "Vector",
values_to = "Mean"
) %>%
ggplot(aes(id, Mean, colour = Vector)) +
geom_point() +
geom_line()
測試數據。
set.seed(1234)
list_size <- 4 # 49 in the question
samp_size <- 20 # 10000 in the question
x <- lapply(seq.int(list_size), function(i) rgamma(samp_size, shape = i))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.