I use "while" loop but it produces an errro. I have no idea about this. Error in "[<-"(`*tmp*`, i, value = numeric(0)) : nothing to replace with The problem description is The likelihood includes two parameters to be estimated: lambda (=beta0+beta1*x) and alpha. The algorithm for the estimation is as following: 1) with alpha=0, estimate lambda (estimate beta0 and beta1 via GLM) 2) with lambda, estimate alpha via ML estimation 3) with updated alpha, replicate 1) and 2) until alpha is converged to a value My source code is alpha=rep(1,100) beta0=rep(0,100) beta1=rep(0,100) lambda1=rep(0,100) lambda2=rep(0,100) verpi=rep(0,100) L=rep(0,100) alpha[0]=1 i=1 while(i<=100){ repeat{ verpi[i]<-c(5^alpha[i-1],10^alpha[i-1]-5^alpha[i-1],14^alpha[i-1]-10^alpha[i-1],18^alpha[i-1]-14^alpha[i-1]) r<-c(1,0,0,1) k<-c(3,2,2,2) x<-c(0.5,0.5,1.0,1.0) GLM_results <- glm(r/k ~ x, family=binomial(link='cloglog'), offset=log(verpi[i]),weights=k) beta0[i]<-GLM_results$coefficients[[1]] beta1[i]<-GLM_results$coefficients[[2]] lambda1[i]<-beta0[i]+beta1[i]*x[1] lambda2[i]<-beta0[i]+beta1[i]*x[3] L[i]<-function(a){ s1_f1<-(exp(-lambda1[i]*(0^a-0^a))-exp(-lambda1[i]*(5^a-0^a))) s2_f2<-(exp(-lambda1[i]*(10^a)-lambda2[i]*(14^a-10^a+14^a-14^a)) -exp(-lambda1[i]*(10^a)-lambda2[i]*(14^a-10^a+18^a-14^a))) logl<-log(s1_f1*s2_f2) return(-logl)} optim(1,L[i]) alpha[i]<-round(optim(1,L[i])$par,2) if(alpha[i]<=alpha[i-1]) break} i=i+1 } Best regards, Jin -- View this message in context: http://www.nabble.com/Error-in-%22-%3C-%22%28%60*tmp*%60%2C-i%2C-value-%3D-numeric%280%29%29-%3A-tp19464723p19464723.html Sent from the R help mailing list archive at Nabble.com.
Prof Brian Ripley
2008-Sep-13 05:53 UTC
[R] Error in "[<-"(`*tmp*`, i, value = numeric(0)) :
On Fri, 12 Sep 2008, sandsky wrote:> > I use "while" loop but it produces an errro. I have no idea about this. > > Error in "[<-"(`*tmp*`, i, value = numeric(0)) : > nothing to replace withWhat version of R is this? I suspect not a recent one. I get Error in verpi[i] <- c(5^alpha[i - 1], 10^alpha[i - 1] - 5^alpha[i - 1], and that tells you the problem. If i = 1, you are looking up alpha[0]. (You also try to set it.) alpha[0] is numeric(0): index '0' means omit the value from the result (see any good book on R).> > The problem description is > > The likelihood includes two parameters to be estimated: lambda > (=beta0+beta1*x) and alpha. The algorithm for the estimation is as > following: > 1) with alpha=0, estimate lambda (estimate beta0 and beta1 via GLM) > 2) with lambda, estimate alpha via ML estimation > 3) with updated alpha, replicate 1) and 2) until alpha is converged to a > value > > My source code is > > alpha=rep(1,100) > beta0=rep(0,100) > beta1=rep(0,100) > lambda1=rep(0,100) > lambda2=rep(0,100) > verpi=rep(0,100) > L=rep(0,100) > > alpha[0]=1 > i=1 > > while(i<=100){ > repeat{ > > verpi[i]<-c(5^alpha[i-1],10^alpha[i-1]-5^alpha[i-1],14^alpha[i-1]-10^alpha[i-1],18^alpha[i-1]-14^alpha[i-1]) > r<-c(1,0,0,1) > k<-c(3,2,2,2) > x<-c(0.5,0.5,1.0,1.0) > GLM_results <- glm(r/k ~ x, family=binomial(link='cloglog'), > offset=log(verpi[i]),weights=k) > beta0[i]<-GLM_results$coefficients[[1]] > beta1[i]<-GLM_results$coefficients[[2]] > lambda1[i]<-beta0[i]+beta1[i]*x[1] > lambda2[i]<-beta0[i]+beta1[i]*x[3] > > L[i]<-function(a){ > s1_f1<-(exp(-lambda1[i]*(0^a-0^a))-exp(-lambda1[i]*(5^a-0^a))) > s2_f2<-(exp(-lambda1[i]*(10^a)-lambda2[i]*(14^a-10^a+14^a-14^a)) > -exp(-lambda1[i]*(10^a)-lambda2[i]*(14^a-10^a+18^a-14^a))) > logl<-log(s1_f1*s2_f2) > return(-logl)} > > optim(1,L[i]) > alpha[i]<-round(optim(1,L[i])$par,2) > if(alpha[i]<=alpha[i-1]) > break} > i=i+1 > } > > Best regards, > > > Jin > -- > View this message in context: http://www.nabble.com/Error-in-%22-%3C-%22%28%60*tmp*%60%2C-i%2C-value-%3D-numeric%280%29%29-%3A-tp19464723p19464723.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595