R语言数据分布参数估计和假设检验

1. R语言内置的数据分布

2. 数据分布参数估计和假设检验

数据分布参数估计和假设检验可以用 R fitdistrplus包的fitdist函数以及MASS包的fitdistr函数实现(极大似然拟合)。

###### MASS包实现#########
library(MASS)
m_data <- rweibull(1000, shape=0.5)
# fit_result <- fitdistr(m_data,'weibull',list(shape=0.1,scale=0.2))
fit_result <- fitdistr(m_data,'weibull')

x <- rgamma(100, shape = 5, rate = 0.1)
fit_result1 <- fitdistr(x, "gamma")
fit_result2 <- fitdistr(m_data,'normal')
fit_result1[4]  # $loglik

###### fitdistrplus包实现#########
library(fitdistrplus)
x <- rgamma(100, shape = 5, rate = 0.1)
fit_result3 <- fitdist(x,'gamma',start=list(shape = 4, rate = 0.2))
fit_result4 <- fitdist(x,'norm')
# fit 结果不是很稳定
fit_result3
fit_result3[6] # $loglik
summary(fit_result3)
# Goodness-of-fit statistics
gofstat(fit_result3)
# p-value 越小,越有理由认为不是同一分布
ks.test(x,'pnorm',mean=fit_result4$estimate[1],sd=fit_result4$estimate[2])
ks.test(x,'pgamma',shape=fit_result3$estimate[1],rate=fit_result3$estimate[2])

require(graphics)
x <- rnorm(500)
y <- runif(300)
# Do x and y come from the same distribution?
# null hypothesis: x and y were drawn from the same continuous distribution 
# p-value 越小,越有理由认为不是同一分布
ks.test(x, y)
# Does x come from a shifted gamma distribution with shape 3 and rate 2?
ks.test(x+2, "pgamma", 3, 2) # two-sided, exact
ks.test(x+2, "pgamma", 3, 2, exact = FALSE)
ks.test(x+2, "pgamma", 3, 2, alternative = "gr")

# test if x is stochastically larger than x2
x2 <- rnorm(50, -1)
plot(ecdf(x), xlim = range(c(x, x2)))
plot(ecdf(x2), add = TRUE, lty = "dashed")
t.test(x, x2, alternative = "g")
wilcox.test(x, x2, alternative = "g")
ks.test(x, x2, alternative = "l")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值