[R] Incompatible types error

Don MacQueen macq at llnl.gov
Sat Feb 6 01:23:30 CET 2010


There appear to be quite a few problems.

The first and most glaring to my eyes is that you initialize four 
vectors, Et, fx, Tx, and Fitness inside the loop. Since you set their 
lengths to 1000, one might assume that the 1000 values are to be 
calculated as the loop index, i, goes from 1 to 1000. But 
initializing them inside the loop each time wipes out any results 
calculated in previous iterations.

Another is that having initialized them to numeric, you immediately 
assign a non-numeric value, that is, Et[i] is defined to be a 
function, not a number.

There are other things, such as defining
   function(Eztx,t,A,n)
and then inside the function immediately setting the value of A to 
Amp, which is defined outside the function. This means that if you 
later use the function, whatever you give to A when you use the 
function will be ignored, and Amp will be used instead.

Then there's the expression runif[i](1,-50,50). But runif is a 
built-in R function, and you really can't use the [i] syntax after a 
function name. For example, suppose i=3. Then
   > runif[3]
    Error in runif[3] : object of type 'closure' is not subsettable


Finally, and of much less importance, the c() around c(1:1000) is 
completely unnecessary. It's fine to just do
   for (i in 1:1000) {


I'm kind of guessing you have a fundamental misunderstanding about 
what a function is and does. I'd suggest starting simple and 
gradually building up to something like this. And please try to find 
some local help with R; from what I see you need more help than can 
be effectively given over email.

-Don

At 12:57 PM -0800 2/5/10, apellegrini001 wrote:
>I'm trying to write a loop for a series of nested functions. and I get an
>incompatible types error when trying to run it. It's supposed to be a
>simulation (1000 iterations) with a random value for "rand" being chosen
>each time. After each rand value is chosen, the rest of the functions are
>evaluated with this given rand value and their "Fitness" sum value is saved
>and plotted against rand.
>
>let me know if you see any obvious problems.
>
>Below is the Script
>"
>#variables
>Amp=3
>n=0.5
>
>#variables
>nt<-200
>z0=1
>z<-rep(z0,nt)
>
>#overlying function
>   t<-seq(0,(2*pi),by=0.01) 
>for(i in c(1:1000)){               
>Et<-numeric(1000)
>fx<-numeric(1000)
>Tx<-numeric(1000)
>Fitness<-numeric(1000)
>  Et[i]<-function(Eztx,t,A,n){
>     A=Amp
>     Eztx[i]<-A*sin(t)*n
>      if(Tx[i]>1){Eztx[i]/Tx[i]
>     } else {Eztx[i]<-Eztx[i]
>   }
>   fx[i]<-function(mea,rand,prob,Et){
>     rand[i]<-runif[i](1,-50,50)
>     mea=0
>     prob[i]<-function(pi,rand,Et,mea){
>     
>(1/(2*pi*rand[i]))*exp((-(Et[i](Eztx[i],t,A,n))-mea)^2)/(2*(rand[i]^2))
>      }
>     prob(pi,rand[i],Et[i](Eztx[i],t,A,n),mea)
>    }
>   Tx[i]<-function(fx,cdist){
>     cdist[i] <-(1-((pnorm(Et[i](Eztx[i],t,A,n),mea,rand[i],))
>      	-(pnorm(mea,mea,rand[i],))))
>     Tzx[i]<-fx[i](prob[i],rand[i],Et[i])*cdist[i]
>     Tzx[i]
>    }
>   }
>Fitness[i]<-function(Tx){
>   f[i]<-Tx[i]
>   Fit[i]<-sum(f[i])
>  }
>}
>plot(Fit,rand)
>
>"
>--
>View this message in context: 
>http://*n4.nabble.com/Incompatible-types-error-tp1470783p1470783.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.


-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062



More information about the R-help mailing list