You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言循环生成多分布值并求和拟合分布的技术问题

Hey there! Let's work through your problem step by step. I'll help you fix the summation issue, verify that UnivarMixingDistribution works correctly, and implement the full workflow of generating samples, summing them, and fitting a distribution to the result.


Step 1: Load Required Packages

First, we'll use the distr package for defining mixed distributions and fitdistrplus for fitting distributions to our summed data. Install them if you haven't already:

# Install packages (run once)
# install.packages(c("distr", "fitdistrplus"))

library(distr)
library(fitdistrplus)

Step 2: Define Your Distributions

Let's specify the two log-normal distributions and the mixed uniform distribution. I'll use example parameters here—you can adjust them to match your needs:

# Define log-normal distributions
lnorm_dist1 <- Lnorm(meanlog = 0, sdlog = 0.5)  # Meanlog and sdlog are parameters for log-normal
lnorm_dist2 <- Lnorm(meanlog = 1, sdlog = 0.3)

# Define mixed uniform distribution: 60% U(0,2), 40% U(5,7)
unif_dist1 <- Unif(min = 0, max = 2)
unif_dist2 <- Unif(min = 5, max = 7)
mixed_unif_dist <- UnivarMixingDistribution(unif_dist1, unif_dist2, mixCoeff = c(0.6, 0.4))

Verify the Mixed Uniform Distribution

To confirm UnivarMixingDistribution is working, generate some samples and plot them:

set.seed(123)  # Keep results reproducible
mixed_unif_samples <- r(mixed_unif_dist)(1000)
hist(mixed_unif_samples, breaks = 20, main = "Mixed Uniform Distribution Samples", col = "lightblue")

You should see two distinct peaks (around 1 and 6), which matches our 60/40 mix of the two uniform distributions—this confirms the function works as expected.

Step 3: Generate Samples and Compute the Sum

The key issue with your original code was likely trying to sum list elements incorrectly. Instead, generate independent sample vectors for each distribution, then add them element-wise:

set.seed(123)
sample_size <- 10000  # Adjust sample size as needed

# Generate samples from each distribution
v1 <- r(lnorm_dist1)(sample_size)
v2 <- r(lnorm_dist2)(sample_size)
v3 <- r(mixed_unif_dist)(sample_size)

# Compute element-wise sum of the three vectors
sum_vector <- v1 + v2 + v3

# Optional: Store all data in a data frame for easy inspection
dist_data <- data.frame(log_normal1 = v1, log_normal2 = v2, mixed_uniform = v3, total_sum = sum_vector)
head(dist_data)

If You Prefer Using Lists

If you want to store the individual distribution samples in a list, you can still compute the element-wise sum by converting the list to a data frame first:

dist_list <- list(v1 = v1, v2 = v2, v3 = v3)
sum_from_list <- rowSums(data.frame(dist_list))  # Same result as sum_vector above

Step 4: Fit a Distribution to the Sum

Now we'll fit a distribution to sum_vector. First, let's explore the shape of our summed data:

# Summarize and visualize the summed distribution
descdist(sum_vector, discrete = FALSE, boot = 1000)

This will show you metrics like skewness and kurtosis, which help you choose a suitable distribution to fit. Let's try fitting a Gamma and log-normal distribution (common choices for positive, skewed data):

# Fit Gamma distribution
fit_gamma <- fitdist(sum_vector, "gamma")
summary(fit_gamma)
plot(fit_gamma)  # Visualize fit vs observed data

# Fit log-normal distribution
fit_lnorm <- fitdist(sum_vector, "lnorm")
summary(fit_lnorm)
plot(fit_lnorm)

Compare Fit Quality

Use AIC (Akaike Information Criterion) to pick the best-fitting distribution—lower AIC means a better fit:

AIC(fit_gamma, fit_lnorm)

内容的提问来源于stack exchange,提问作者Kristian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:35:54