Re: [R] how do I transform this to a for loop

2010-09-07 Thread Setlhare Lekgatlhamang
Hi Bill,
There is a problem with the for loop. When I copied and pasted in my R
console it could not run, instead I got an error message "Error in
NCOL(x) : object 'data.ts' not found".

Lexi


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of bill.venab...@csiro.au
Sent: Monday, September 06, 2010 6:17 AM
To: gaut...@yahoo.com; r-help@r-project.org
Subject: Re: [R] how do I transform this to a for loop


sseq <- c(1, seq(5, 40, by = 5))
for(i in 1:length(sseq)) 
assign(paste("arima", i, sep=""), arima(data.ts[sseq[i]:(sseq[i]+200)],
order=c(1,1,1)))

...but why would you want to do so?


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of lord12
Sent: Monday, 6 September 2010 10:57 AM
To: r-help@r-project.org
Subject: [R] how do I transform this to a for loop


arima1 <- arima(data.ts[1:201], order = c(1,1,1))
arima2 <- arima(data.ts[5:205], order = c(1,1,1))
arima3 <- arima(data.ts[10:210], order = c(1,1,1))
arima4 <- arima(data.ts[15:215], order = c(1,1,1))
arima5 <- arima(data.ts[20:220], order = c(1,1,1))
arima6 <- arima(data.ts[25:225], order = c(1,1,1))
arima7 <- arima(data.ts[30:230], order = c(1,1,1))
arima8 <- arima(data.ts[35:235], order = c(1,1,1))
arima9 <- arima(data.ts[40:240], order = c(1,1,1))

-- 
View this message in context:
http://r.789695.n4.nabble.com/how-do-I-transform-this-to-a-for-loop-tp25
27816p2527816.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.

__
R-help@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.



DISCLAIMER:\ Sample Disclaimer added in a VBScript.\ ...{{dropped:3}}

__
R-help@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.


Re: [R] how do I transform this to a for loop

2010-09-07 Thread Karl Brand

I think i've  got it:

for() is best employed for convenience only; and best NOT employed in 
computation for which many other approaches/functions are better suited 
(faster).


Many thanks for your comment,

Karl

On 9/7/2010 12:52 AM, bill.venab...@csiro.au wrote:

I have in mind the following quote from John Chambers

Tradition among experienced S programmers has always been that loops (typically 
'for'
loops) are intrinsically inefficient: expressing computations without loops has 
provided
a measure of entry into the inner circle of S programming.
-- John Chambers
   Programming With Data, p. 173 (1998)

If I had to do a series of computations like this I would actually use the loop 
to create the code and then run it as a batch job.  Loops can be very slow 
because of 'backout'.

Try the following little script, which take about no time at all.


comm<- quote(M<- arima(data.ts[START:FINISH], order = c(1,1,1)))
subst<- function(Command, ...) do.call("substitute", list(Command, list(...)))

sink("fitArmias.R")
for(i in 1:100) {
thisComm<- subst(comm,
M = as.name(paste("arima", substring(1000+i, 2), sep = "")),
START = 0+i, FINISH = 200+i)
print(thisComm)
}
sink()


and then look at the file fitArimas.R.  If you were to run this as a batch job 
- the way I would do things - it would work *much* faster than doing the same 
thing in a for() loop the way I suggested.

Just a comment, of course.

Bill Venables.

-Original Message-
From: Karl Brand [mailto:k.br...@erasmusmc.nl]
Sent: Monday, 6 September 2010 6:46 PM
To: Venables, Bill (CMIS, Cleveland)
Cc: gaut...@yahoo.com; r-help@r-project.org
Subject: Re: [R] how do I transform this to a for loop

Hi Bill,

I didn't make the original post, but its pretty similar to some thing i
would have queried the list about. But, as an R dilatante i find more
curious your question-

"...but why would you want to do so?"

Is this because you'd typically use the given nine lines of explicit
code to carve up a single dataset into nine symmetrical variants ? Or
that some contextual information may affect how you would write the
for() loop?

As i lack the experience to know any better, i perceive your for() loop
as de rigour in efficient use of R, and the preferance of all
experienced R user's. But not having any formal education in R or role
models as such, its only an assumption (compeletely ignoring for the
moment processing efficiency/speed, rounding error and such).

But which i now question! Explicit, simple crude looking code; or,
something which demands a little more proficiency with the language?

cheers,

Karl



On 9/6/2010 6:16 AM, bill.venab...@csiro.au wrote:


sseq<- c(1, seq(5, 40, by = 5))
for(i in 1:length(sseq))
assign(paste("arima", i, sep=""), arima(data.ts[sseq[i]:(sseq[i]+200)], 
order=c(1,1,1)))

...but why would you want to do so?


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of lord12
Sent: Monday, 6 September 2010 10:57 AM
To: r-help@r-project.org
Subject: [R] how do I transform this to a for loop


arima1<- arima(data.ts[1:201], order = c(1,1,1))
arima2<- arima(data.ts[5:205], order = c(1,1,1))
arima3<- arima(data.ts[10:210], order = c(1,1,1))
arima4<- arima(data.ts[15:215], order = c(1,1,1))
arima5<- arima(data.ts[20:220], order = c(1,1,1))
arima6<- arima(data.ts[25:225], order = c(1,1,1))
arima7<- arima(data.ts[30:230], order = c(1,1,1))
arima8<- arima(data.ts[35:235], order = c(1,1,1))
arima9<- arima(data.ts[40:240], order = c(1,1,1))





--
Karl Brand 
Department of Genetics
Erasmus MC
Dr Molewaterplein 50
3015 GE Rotterdam
P +31 (0)10 704 3409 | F +31 (0)10 704 4743 | M +31 (0)642 777 268

__
R-help@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.


Re: [R] how do I transform this to a for loop

2010-09-06 Thread Karl Brand

Hi Paul, Ivan,

Hartstikke bedankt and thanks alot for sharing these thoughts. I can see 
'listing up' multiple symmetrical data sets makes a lot of sense. As 
does using lapply() on them which i understand to be more 
efficient/faster than for().


Goodo- with your concensus (and helpful examples) i can tell myself 
investing the extra time to use lapply on lists /will/ pay off in the 
long run vs. copying and pasting (nearly) the same line of code 10 times 
for every data manipulation...


thanks again,

Karl



On 9/6/2010 12:09 PM, Paul Hiemstra wrote:

Hi Karl,

The "why do it like this" is probably direct towards creating 9 new
objects for the arima results (Is this right Bill?). A better option
would be to create a list with nine entries. This is much easier for any
subsequent analyses. An example that uses lapply (an efficient syntax
for loops):

sseq <- c(1, seq(5, 40, by = 5))
result_list = lapply(sseq, function(num) {
arima(data.ts[num:(num+200)], order=c(1,1,1))
})

cheers,
Paul

On 09/06/2010 10:46 AM, Karl Brand wrote:

Hi Bill,

I didn't make the original post, but its pretty similar to some thing
i would have queried the list about. But, as an R dilatante i find
more curious your question-

"...but why would you want to do so?"

Is this because you'd typically use the given nine lines of explicit
code to carve up a single dataset into nine symmetrical variants ? Or
that some contextual information may affect how you would write the
for() loop?

As i lack the experience to know any better, i perceive your for()
loop as de rigour in efficient use of R, and the preferance of all
experienced R user's. But not having any formal education in R or role
models as such, its only an assumption (compeletely ignoring for the
moment processing efficiency/speed, rounding error and such).

But which i now question! Explicit, simple crude looking code; or,
something which demands a little more proficiency with the language?

cheers,

Karl



On 9/6/2010 6:16 AM, bill.venab...@csiro.au wrote:


sseq<- c(1, seq(5, 40, by = 5))
for(i in 1:length(sseq))
assign(paste("arima", i, sep=""),
arima(data.ts[sseq[i]:(sseq[i]+200)], order=c(1,1,1)))

...but why would you want to do so?


-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of lord12
Sent: Monday, 6 September 2010 10:57 AM
To: r-help@r-project.org
Subject: [R] how do I transform this to a for loop


arima1<- arima(data.ts[1:201], order = c(1,1,1))
arima2<- arima(data.ts[5:205], order = c(1,1,1))
arima3<- arima(data.ts[10:210], order = c(1,1,1))
arima4<- arima(data.ts[15:215], order = c(1,1,1))
arima5<- arima(data.ts[20:220], order = c(1,1,1))
arima6<- arima(data.ts[25:225], order = c(1,1,1))
arima7<- arima(data.ts[30:230], order = c(1,1,1))
arima8<- arima(data.ts[35:235], order = c(1,1,1))
arima9<- arima(data.ts[40:240], order = c(1,1,1))








--
Karl Brand 
Department of Genetics
Erasmus MC
Dr Molewaterplein 50
3015 GE Rotterdam
P +31 (0)10 704 3409 | F +31 (0)10 704 4743 | M +31 (0)642 777 268

__
R-help@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.


Re: [R] how do I transform this to a for loop

2010-09-06 Thread Ivan Calandra
  Hi Karl,

I think the question here is why would you want to create different 
objects in the loop using assign().
Usually, using lists is better (more efficient?), although I sometimes 
use assign() too in this context. I do it when I want to export the 
object as separate files (xls, Rbin, svg, etc). If I don't need separate 
files, I use lists.
I'm no expert so I'm not even sure I use the "correct" approach, but it 
might help you get a better understanding.

It would be something like:
sseq<- c(1, seq(5, 40, by = 5))
arima <- vector(mode="list", length=length(sseq)) ##not sure it is 
necessary, but it might be
for(i in 1:length(sseq)) {
arima[[i]] <- arima(data.ts[sseq[i]:(sseq[i]+200)], order=c(1,1,1))
}

HTH,
Ivan


Le 9/6/2010 10:46, Karl Brand a écrit :
> Hi Bill,
>
> I didn't make the original post, but its pretty similar to some thing 
> i would have queried the list about. But, as an R dilatante i find 
> more curious your question-
>
> "...but why would you want to do so?"
>
> Is this because you'd typically use the given nine lines of explicit 
> code to carve up a single dataset into nine symmetrical variants ? Or 
> that some contextual information may affect how you would write the 
> for() loop?
>
> As i lack the experience to know any better, i perceive your for() 
> loop as de rigour in efficient use of R, and the preferance of all 
> experienced R user's. But not having any formal education in R or role 
> models as such, its only an assumption (compeletely ignoring for the 
> moment processing efficiency/speed, rounding error and such).
>
> But which i now question! Explicit, simple crude looking code; or, 
> something which demands a little more proficiency with the language?
>
> cheers,
>
> Karl
>
>
>
> On 9/6/2010 6:16 AM, bill.venab...@csiro.au wrote:
>>
>> sseq<- c(1, seq(5, 40, by = 5))
>> for(i in 1:length(sseq))
>> assign(paste("arima", i, sep=""), 
>> arima(data.ts[sseq[i]:(sseq[i]+200)], order=c(1,1,1)))
>>
>> ...but why would you want to do so?
>>
>>
>> -Original Message-
>> From: r-help-boun...@r-project.org 
>> [mailto:r-help-boun...@r-project.org] On Behalf Of lord12
>> Sent: Monday, 6 September 2010 10:57 AM
>> To: r-help@r-project.org
>> Subject: [R] how do I transform this to a for loop
>>
>>
>> arima1<- arima(data.ts[1:201], order = c(1,1,1))
>> arima2<- arima(data.ts[5:205], order = c(1,1,1))
>> arima3<- arima(data.ts[10:210], order = c(1,1,1))
>> arima4<- arima(data.ts[15:215], order = c(1,1,1))
>> arima5<- arima(data.ts[20:220], order = c(1,1,1))
>> arima6<- arima(data.ts[25:225], order = c(1,1,1))
>> arima7<- arima(data.ts[30:230], order = c(1,1,1))
>> arima8<- arima(data.ts[35:235], order = c(1,1,1))
>> arima9<- arima(data.ts[40:240], order = c(1,1,1))
>>
>

-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] how do I transform this to a for loop

2010-09-06 Thread Paul Hiemstra

Hi Karl,

The "why do it like this" is probably direct towards creating 9 new 
objects for the arima results (Is this right Bill?). A better option 
would be to create a list with nine entries. This is much easier for any 
subsequent analyses. An example that uses lapply (an efficient syntax 
for loops):


sseq <- c(1, seq(5, 40, by = 5))
result_list = lapply(sseq, function(num) {
arima(data.ts[num:(num+200)], order=c(1,1,1))
})

cheers,
Paul

On 09/06/2010 10:46 AM, Karl Brand wrote:

Hi Bill,

I didn't make the original post, but its pretty similar to some thing 
i would have queried the list about. But, as an R dilatante i find 
more curious your question-


"...but why would you want to do so?"

Is this because you'd typically use the given nine lines of explicit 
code to carve up a single dataset into nine symmetrical variants ? Or 
that some contextual information may affect how you would write the 
for() loop?


As i lack the experience to know any better, i perceive your for() 
loop as de rigour in efficient use of R, and the preferance of all 
experienced R user's. But not having any formal education in R or role 
models as such, its only an assumption (compeletely ignoring for the 
moment processing efficiency/speed, rounding error and such).


But which i now question! Explicit, simple crude looking code; or, 
something which demands a little more proficiency with the language?


cheers,

Karl



On 9/6/2010 6:16 AM, bill.venab...@csiro.au wrote:


sseq<- c(1, seq(5, 40, by = 5))
for(i in 1:length(sseq))
assign(paste("arima", i, sep=""), 
arima(data.ts[sseq[i]:(sseq[i]+200)], order=c(1,1,1)))


...but why would you want to do so?


-Original Message-
From: r-help-boun...@r-project.org 
[mailto:r-help-boun...@r-project.org] On Behalf Of lord12

Sent: Monday, 6 September 2010 10:57 AM
To: r-help@r-project.org
Subject: [R] how do I transform this to a for loop


arima1<- arima(data.ts[1:201], order = c(1,1,1))
arima2<- arima(data.ts[5:205], order = c(1,1,1))
arima3<- arima(data.ts[10:210], order = c(1,1,1))
arima4<- arima(data.ts[15:215], order = c(1,1,1))
arima5<- arima(data.ts[20:220], order = c(1,1,1))
arima6<- arima(data.ts[25:225], order = c(1,1,1))
arima7<- arima(data.ts[30:230], order = c(1,1,1))
arima8<- arima(data.ts[35:235], order = c(1,1,1))
arima9<- arima(data.ts[40:240], order = c(1,1,1))






--
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone:  +3130 253 5773
http://intamap.geo.uu.nl/~paul
http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770

__
R-help@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.


Re: [R] how do I transform this to a for loop

2010-09-06 Thread Karl Brand

Hi Bill,

I didn't make the original post, but its pretty similar to some thing i 
would have queried the list about. But, as an R dilatante i find more 
curious your question-


"...but why would you want to do so?"

Is this because you'd typically use the given nine lines of explicit 
code to carve up a single dataset into nine symmetrical variants ? Or 
that some contextual information may affect how you would write the 
for() loop?


As i lack the experience to know any better, i perceive your for() loop 
as de rigour in efficient use of R, and the preferance of all 
experienced R user's. But not having any formal education in R or role 
models as such, its only an assumption (compeletely ignoring for the 
moment processing efficiency/speed, rounding error and such).


But which i now question! Explicit, simple crude looking code; or, 
something which demands a little more proficiency with the language?


cheers,

Karl



On 9/6/2010 6:16 AM, bill.venab...@csiro.au wrote:


sseq<- c(1, seq(5, 40, by = 5))
for(i in 1:length(sseq))
assign(paste("arima", i, sep=""), arima(data.ts[sseq[i]:(sseq[i]+200)], 
order=c(1,1,1)))

...but why would you want to do so?


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of lord12
Sent: Monday, 6 September 2010 10:57 AM
To: r-help@r-project.org
Subject: [R] how do I transform this to a for loop


arima1<- arima(data.ts[1:201], order = c(1,1,1))
arima2<- arima(data.ts[5:205], order = c(1,1,1))
arima3<- arima(data.ts[10:210], order = c(1,1,1))
arima4<- arima(data.ts[15:215], order = c(1,1,1))
arima5<- arima(data.ts[20:220], order = c(1,1,1))
arima6<- arima(data.ts[25:225], order = c(1,1,1))
arima7<- arima(data.ts[30:230], order = c(1,1,1))
arima8<- arima(data.ts[35:235], order = c(1,1,1))
arima9<- arima(data.ts[40:240], order = c(1,1,1))



--
Karl Brand 
Department of Genetics
Erasmus MC
Dr Molewaterplein 50
3015 GE Rotterdam
P +31 (0)10 704 3409 | F +31 (0)10 704 4743 | M +31 (0)642 777 268

__
R-help@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.


Re: [R] how do I transform this to a for loop

2010-09-05 Thread Bill.Venables

sseq <- c(1, seq(5, 40, by = 5))
for(i in 1:length(sseq)) 
assign(paste("arima", i, sep=""), arima(data.ts[sseq[i]:(sseq[i]+200)], 
order=c(1,1,1)))

...but why would you want to do so?


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of lord12
Sent: Monday, 6 September 2010 10:57 AM
To: r-help@r-project.org
Subject: [R] how do I transform this to a for loop


arima1 <- arima(data.ts[1:201], order = c(1,1,1))
arima2 <- arima(data.ts[5:205], order = c(1,1,1))
arima3 <- arima(data.ts[10:210], order = c(1,1,1))
arima4 <- arima(data.ts[15:215], order = c(1,1,1))
arima5 <- arima(data.ts[20:220], order = c(1,1,1))
arima6 <- arima(data.ts[25:225], order = c(1,1,1))
arima7 <- arima(data.ts[30:230], order = c(1,1,1))
arima8 <- arima(data.ts[35:235], order = c(1,1,1))
arima9 <- arima(data.ts[40:240], order = c(1,1,1))

-- 
View this message in context: 
http://r.789695.n4.nabble.com/how-do-I-transform-this-to-a-for-loop-tp2527816p2527816.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.

__
R-help@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.