data=c(2,3,2,2,2,1,2,2,2,4,3,2,1,4,3,1,2,1,3,2,2,2,1,2,4,1,1,3,1,2,1,2,2,2,2,2,2,3,2,1) ## population data
X=matrix(data,4) ## four samples each of size 10
X
我想通过此分布估算每行(每个样本)数据的参数。
DGIWD=function(x,t1,a1,b1) t1^(b1/(x+1))^a1- t1^(b1/(x))^a1
library(MASS)
jk<-fitdistr(x =X[1,],densfun = DGIWD,start = list(t1=0.6,a1 =3,b1=2.3)) ### estimates parameters for first row X[1,]
jk
jk$estimate[1] ## first estimate
jk$estimate[2] ## 2nd estimate
jk$estimate[3] ## 3rd estimate
我有兴趣运行for循环来估计参数,我想将参数存储在矩阵中 共有4个样本,参数是3,所以我想将参数存储在4X3矩阵中,如下所示
estimates<-matrix(NA,nrow=4,ncol=3,byrow=TRUE)
estimates
你能帮忙,如何处理这种情况。
我自己现在尝试了
h=function(X){
DGIWD=function(x,t1,a1,b1) t1^(b1/(x+1))^a1- t1^(b1/(x))^a1
library(MASS)
jk<-fitdistr(x =X,densfun = DGIWD,start = list(t1=0.6,a1 =3,b1=2.3)) ### estimates parameters for first row X[1,]
jk
}
X <-list(1:3,4:6,7:9)
data=c(2,3,2,2,2,1,2,2,2,4,3,2,1,4,3,1,2,1,3,2,2,2,1,2,4,1,1,3,1,2,1,2,2,2,2,2,2,3,2,1) ## population data
X=matrix(data,4) ## four samples in five rows
apply(X,1,h)
a=(do.call(rbind,apply(X,1,h)))[,1]
b=as.matrix.data.frame(unlist(a))
但仍然无法获得4X3矩阵
答案 0 :(得分:0)
请仔细阅读以下R代码:
data=c(2,3,2,2,2,1,2,2,2,4,3,2,1,4,3,1,2,1,3,2,2,2,1,2,4,1,1,3,1,2,1,2,2,2,2,2,2,3,2,1) ## population data
X=matrix(data,4) ## four samples each of size 10
DGIWD=function(x,t1,a1,b1) t1^(b1/(x+1))^a1- t1^(b1/(x))^a1
library(MASS)
jk=c() #Create empty vector
for(i in 1:nrow(X)){
a=fitdistr(x =X[i,],densfun = DGIWD,start = list(t1=0.6,a1=3,b1=2.3))
jk=c(jk,a$estimate) #Append the result to vector jk
}
final_matrix=matrix(jk,nrow = nrow(X),byrow = TRUE) # Convert vector to matrix
以上代码的输出如下所示:
> X
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 2 2 2 1 2 2 4 1 2 2
[2,] 3 1 4 4 1 2 1 2 2 3
[3,] 2 2 3 3 3 1 1 1 2 2
[4,] 2 2 2 1 2 2 3 2 2 1
> jk
t1 a1 b1 t1 a1 b1 t1 a1 b1
0.5353273 5.6575948 2.3487268 0.5601999 2.9350104 2.6272016 0.5550104 4.0996826 2.4168878
t1 a1 b1
0.5582317 7.0411706 2.3134498
> final_matrix
[,1] [,2] [,3]
[1,] 0.5353273 5.657595 2.348727
[2,] 0.5601999 2.935010 2.627202
[3,] 0.5550104 4.099683 2.416888
[4,] 0.5582317 7.041171 2.313450
在上面的输出中,final_matrix
是您想要的输出,尺寸为4X3。希望它适合你!