我想在我创建的数据样本上运行一些最大似然代码。这就是我到目前为止所做的:
library("maxLik")
data <- replicate(20, rnorm(100))
logLikFun <- function(param) {
mu <- param[1]
sigma <- param[2]
sum(dnorm(data, mean = mu, sd = sigma, log = TRUE))
}
mle <- maxLik(logLik = logLikFun, start = c(mu = 0, sigma = 1))
summary(mle)
我在提取20的每个样本的均值和标准差时遇到一些问题,我修改了apply函数以试图适应这个但是还没有任何工作。有任何想法吗?
答案 0 :(得分:5)
创建一个函数(在此示例中为find.mle
),它接收数据向量并根据它计算MLE,然后使用apply
将其应用于data
列:
library("maxLik")
data <- replicate(20, rnorm(100))
find.mle = function(d) {
logLikFun <- function(param) {
mu <- param[1]
sigma <- param[2]
sum(dnorm(d, mean = mu, sd = sigma, log = TRUE))
}
maxLik(logLik = logLikFun, start = c(mu = 0, sigma = 1))$estimate
}
mles = apply(data, 2, find.mle)
这将为您提供2x20矩阵与您的估算:
> mles
[,1] [,2] [,3] [,4] [,5] [,6]
mu 0.03675611 0.1129927 -0.06499549 0.04651673 0.06593217 -0.08753828
sigma 0.93497523 0.9817961 0.84734600 0.93139761 1.01083924 1.04114752
[,7] [,8] [,9] [,10] [,11] [,12]
mu 0.1629807 0.01665411 0.2306688 -0.02147982 0.07723695 0.009476477
sigma 1.0428713 1.01658241 1.0073277 0.99781761 0.99327722 0.983356049
[,13] [,14] [,15] [,16] [,17] [,18]
mu 0.06524147 0.02442983 -0.1305258 -0.1050299 0.1449996 0.1172218
sigma 1.04004799 0.89963009 0.9979824 1.0227063 0.9319562 0.9916734
[,19] [,20]
mu -0.1288296 -0.05769467
sigma 0.9975368 0.89506586
答案 1 :(得分:1)
我真的认为不需要编写任何函数来获得均值和sd的最大似然(ML以下)估计。如果X是正态随机变量,则总体均值和sd的ML估计量是样本均值和样本sd,并且我们知道样本均值是对总体均值的无偏估计,但是方差的ML估计是有偏差的。 (向下),因为方差的分母是n而不是n-1。
因此,R计算样本的准方差(校正自由度),这是无偏估计,因此它不是ML估计,但我们可以从R估计中获得ML估计,只是我们只有将它乘以(n-1)(1 / n),结果将是方差的ML估计,然后应用平方根和voilá你将得到sd的ML估计,但我喜欢容易所以,只需将sd乘以(n-1)(1 / n),这就是你的答案。有关详细说明,请参阅http://en.wikipedia.org/wiki/Variance
上的人口差异和样本差异现在你可以简单地在R中执行以下操作:
## Reproducing @ David Robinson code
install.packages('maxLik')
library("maxLik")
set.seed(007) ## making it reproducible
data <- replicate(20, rnorm(100))
find.mle = function(d) {
logLikFun <- function(param) {
mu <- param[1]
sigma <- param[2]
sum(dnorm(d, mean = mu, sd = sigma, log = TRUE))
}
maxLik(logLik = logLikFun, start = c(mu = 0, sigma = 1))$estimate
}
mles = apply(data, 2, find.mle)
apply(data, 2, function(x) c(Mean=mean(x), SD=(n-1)*(1/n)*sd(x))) # my simple answer.
# Comparing results:
> mles
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
mu 0.1386966 0.1304418 -0.03515036 -0.05065659 0.04170382 0.0007424064 -0.07625412
sigma 0.9540009 0.9442371 1.07218240 1.03162817 0.96140925 1.0274500157 0.87450358
[,8] [,9] [,10] [,11] [,12] [,13] [,14]
mu 0.02024026 -0.1732926 0.03401213 -0.1254751 0.05263887 -0.01258275 -0.02843866
sigma 0.98456202 0.9628233 0.95087131 0.9912367 1.01347266 0.99542339 1.03761674
[,15] [,16] [,17] [,18] [,19] [,20]
mu 0.02441331 -0.03021781 0.2170172 0.02271656 -0.04946737 0.115728
sigma 1.03889635 1.02796932 1.0457951 1.07906578 0.93627993 1.009641
> apply(data, 2, function(x) c(Mean=mean(x), SD=(n-1)*(1/n)*sd(x)))
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
Mean 0.1386966 0.1304418 -0.03515036 -0.05065659 0.04170382 0.0007424064 -0.07625412
SD 0.9492189 0.9395041 1.06680802 1.02645707 0.95659012 1.0222998579 0.87012008
[,8] [,9] [,10] [,11] [,12] [,13] [,14]
Mean 0.02024026 -0.1732926 0.03401213 -0.1254751 0.05263887 -0.01258275 -0.02843866
SD 0.97962684 0.9579971 0.94610501 0.9862680 1.00839257 0.99043377 1.03241563
[,15] [,16] [,17] [,18] [,19] [,20]
Mean 0.02441331 -0.03021781 0.2170172 0.02271656 -0.04946737 0.115728
SD 1.03368881 1.02281656 1.0405530 1.07365689 0.93158677 1.004580
如果您只使用一个简单的产品,那么您可以删除该功能(由@David Robinson编写的非常好的功能)。这是一个简单的理论统计观点。