Quantcast

Repeating

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Repeating

efulas
Dear All,


I have a codes which calculates the result of Ripley's K function of my data. I want to repeat this process 999 times. However, i am getting an error when i use the "for i in" function. Is there any way to repeat this analysis 999 times. Here are the codes i used ;


data4 <- matrix(c(sample(id),data1),203,3)

a <- data4[,1]
random.case=data4[a==0,]
random.contr=data4[a==1,]

random.case.locations<-list(x=random.case[,1],y=random.case[,2])
ppregion(xl=min(random.case[,1])-0.0001,xu=max(random.case[,1])+0.0001,yl=min(random.case[,2])-0.0001,yu=max(random.case[,2])+0.0001)
random.l.case <- Kfn(random.case.locations,0.01)

random.k.case <- ((random.l.case$y)^2)/pi
random.contr.locations<-list(x=random.contr[,1],y=random.contr[,2])
ppregion(xl=min(random.contr[,1])-0.0001,xu=max(random.contr[,1])+0.0001,yl=min(random.contr[,2])-0.0001,yu=max(random.contr[,2])+0.0001)
random.l.contr <- Kfn(random.contr.locations,0.01)


random.k.contr <- ((random.l.contr$y)^2)/pi

result <- random.k.case-random.k.contr




Many Thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Repeating

efulas
By the way, my "for" function is below, I can't find the mistake


rand.max.t<- function(n){
f<-rep(NA,n)

for (i in 1:n) {
reassign[i]<-matrix(c(sample(id),data1),203,3)
new.data<-reassign[,1]
random.cas=reassign[new.data==0,2:3]
random.con=reassign[new.data==1,2:3]
f<- list(x=random.cas[,1],y=random.cas[,2])
ppregion(xl=min(random.cas[,1])-0.0001,xu=max(random.cas[,1])+0.0001,yl=min(random.cas[,2])-0.0001,yu=max(random.cas[,2])+0.0001)
g <- Kfn(f,0.01)

random.g <- ((g$y)^2)/pi
}
random.g
}

rand.max.t(10)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Repeating

Rui Barradas
Hello,

efulas wrote
By the way, my "for" function is below, I can't find the mistake


rand.max.t<- function(n){
f<-rep(NA,n)

for (i in 1:n) {
reassign[i]<-matrix(c(sample(id),data1),203,3)
new.data<-reassign[,1]
random.cas=reassign[new.data==0,2:3]
random.con=reassign[new.data==1,2:3]
f<- list(x=random.cas[,1],y=random.cas[,2])
ppregion(xl=min(random.cas[,1])-0.0001,xu=max(random.cas[,1])+0.0001,yl=min(random.cas[,2])-0.0001,yu=max(random.cas[,2])+0.0001)
g <- Kfn(f,0.01)

random.g <- ((g$y)^2)/pi
}
random.g
}

rand.max.t(10)
What is the error message? Without it it's not possible to say, exactly, but:

1. What is the varible 'reassign'? You use it before it's created. The instruction

    reassign[i]<-matrix( ...etc...

    changes a value that should already exist. This should produce an error.
    (By seeing how 'reassign' is used, you're creating a matrix, get rid of the index in reassign[i].)

2. What is 'f'? You're creating it, then recreating it but never using it. Get rid of it.

3. Every time through the loop you attribute a new value to 'random.g', and only the last one is
    returned by the function. If you want all n values, this would be better:

    random.g <- vector("list", n)
    for(i in seq.int(n)){

        [... etc ...]

        random.g[[ i ]] <- ((g$y)^2)/pi
    }


Hope this helps,

Rui Barradas
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Repeating

Michael Weylandt
In reply to this post by efulas
I don't see anything that looks like it should throw an error, but I
haven't tested your code without "data1" and "id" -- you might look at
? replicate() though -- it's designed for these sorts of things. E.g.,

replicate(100, mean(rexp(50)))

gets me a hundred draws of the mean of 50 random exponential variates
(could be done faster here with some matrix tricks, but that's the
idea)

Michael

On Mon, May 7, 2012 at 6:28 AM, efulas <[hidden email]> wrote:

> By the way, my "for" function is below, I can't find the mistake
>
>
> rand.max.t<- function(n){
> f<-rep(NA,n)
>
> for (i in 1:n) {
> reassign[i]<-matrix(c(sample(id),data1),203,3)
> new.data<-reassign[,1]
> random.cas=reassign[new.data==0,2:3]
> random.con=reassign[new.data==1,2:3]
> f<- list(x=random.cas[,1],y=random.cas[,2])
> ppregion(xl=min(random.cas[,1])-0.0001,xu=max(random.cas[,1])+0.0001,yl=min(random.cas[,2])-0.0001,yu=max(random.cas[,2])+0.0001)
> g <- Kfn(f,0.01)
>
> random.g <- ((g$y)^2)/pi
> }
> random.g
> }
>
> rand.max.t(10)
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Repeating-tp4614371p4614482.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Repeating

David Winsemius
In reply to this post by efulas

On May 7, 2012, at 6:28 AM, efulas wrote:

> By the way, my "for" function is below, I can't find the mistake
>
>
> rand.max.t<- function(n){
> f<-rep(NA,n)
>
> for (i in 1:n) {
> reassign[i]<-matrix(c(sample(id),data1),203,3)
> new.data<-reassign[,1]
> random.cas=reassign[new.data==0,2:3]
> random.con=reassign[new.data==1,2:3]
> f<- list(x=random.cas[,1],y=random.cas[,2])
> ppregion(xl=min(random.cas[,1])-0.0001,xu=max(random.cas[,1])
> +0.0001,yl=min(random.cas[,2])-0.0001,yu=max(random.cas[,2])+0.0001)
> g <- Kfn(f,0.01)

I don't know what you are attempting and you include no comments in  
your code, but 'g' and then 'random.g' would appear to be overwritten  
every time the loop executes. Was that your goal or expectation?

>
> random.g <- ((g$y)^2)/pi
> }
> random.g
> }
>
> rand.max.t(10)
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Repeating-tp4614371p4614482.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Repeating

efulas
Thank you for replies. I sort out the problem by defining  the reassign matrix.



Best Wishes,

efulas
Loading...