0

我有一个带有观察变量的数据框和运行数千行的日期戳 [Var1, DD, MM, YYYY]。我需要为每年的观察变量拟合分布[指数或伽马],并获得每年的相关参数。

在 Matlab 中,它只是

 j=1
 k=1

 for i=1:(no_of_rows-1)

    if  year(i+1) = year(i)
        temp_data_year(j) = Var1(i)
        j=j+1

    else  [a,b]= gamfit(temp)
         param(:,:,k) = [a,b]
         k=k+1
   endif

 end   

所以我会在变量param的数据中获取每年的参数。

那么R中有什么东西可以做到这一点吗?

谢谢,

4

1 回答 1

0

像这样。

# creates a sample dataset - you have this already
set.seed(1)             # for reproducible example
df <- data.frame(var1=c(rgamma(365,2,4),rgamma(365,3,5),rgamma(365,1,8)),
                 YYYY=rep(2012:2014,each=365))

# you start here...
library(fitdistrplus)   # for fitdist(...)
aggregate(var1~YYYY,df,function(X)fitdist(X,distr="gamma")$estimate)
#   YYYY var1.shape var1.rate
# 1 2012   1.891706  3.873906
# 2 2013   2.812962  4.778191
# 3 2014   1.031067  7.826776

阅读包fitdist(...)中的文档fitdistrplus。有几种拟合算法可用。

于 2014-07-18T15:12:47.430 回答