[R] loop for, error: obj type 'closure' not subsetable

Rui Barradas ruipbarradas at sapo.pt
Thu Jul 26 11:50:18 CEST 2012


Hello,

Simple, look carefully at what you have, rho is a function (type 
closure), the vector is rhoes.
And you are trying to print rho[i], the i-th function in the "vector" 
called rho.

print( rhoes[i] )  # works

Hope this helps,

Rui Barradas

Em 26-07-2012 06:40, Rafael_Leon escreveu:
> Hi everyone,
>
> I've got the following problem:
>
> I've got a matrix [1000,2] and two vectors. In very matrix row there is  two
> coefficients b0 and b1. The vectors are two variables x and y.  I want to do
> a loop to take b0 and b1 and with x and y calculate the residual of a linear
> model and calculate the second order coefficient of autocorrelation. What I
> did is :
>
> rho<-function(mat, x,y){
> rhoes=rep(0,dim(mat)[1])
> for (i in 1:dim(mat)[1]){
>          
>             
> rhoes[i]=acf(y-(mat[i,1]+mat[i,2]*x),type="correlation",plot=FALSE)$acf[2]
> #explanation below
>
>      }
>      print(rho[i])
>
> }
>
> I'm getting this error:
> Error in rho[i] : object of type 'closure' is not subsettable
>
> Any help will be deeply appreciated,
>
> Cheers,
>
> Rafael
>
> P.S.
> mat[i,1]=b0
> mat[i,2]=b1
> mat[i,1]+mat[i,2]*x= y estimated
> y-(mat[i,1]+mat[i,2]*x)= errors (residuals)
> acf= R function to calculate autocorrelation
> $acf[2] = to extract 2nd order autocorrelation
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/loop-for-error-obj-type-closure-not-subsetable-tp4637873.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.



More information about the R-help mailing list