Loop

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Loop

Florian Weiler
Dear all,

I have a (probably very basic) question. I am imputing data with the mice package, using 10 chains. I can then write out the 10 final values of the chains simply by

name1 <- complete(imp, 1)
                      :
                      :
name10 <- complete(imp,10)

Not a big deal, I just wanted to do that in a little loop as follows:

for (i in 1:10){
  set[i] <- complete(imp,i)}

Yet that doesn't work, I also tried other things like:
for (i in 1:10){
  set[[i]] <- complete(imp,i)}

Again, no success. It only saves a couple of lines of code, but there must be an easy solution, right?
Thanks,
Florian
Reply | Threaded
Open this post in threaded view
|

Re: Loop

Helios de Rosario


--
Helios de Rosario Martínez
 
 Researcher
>>> El día 22/02/2012 a las 11:32, Florian Weiler <[hidden email]>
escribió:
> Dear all,
>
> I have a (probably very basic) question. I am imputing data with the
mice
> package, using 10 chains. I can then write out the 10 final values of
the
> chains simply by
>
> name1 <- complete(imp, 1)
>                       :
>                       :
> name10 <- complete(imp,10)
>
> Not a big deal, I just wanted to do that in a little loop as
follows:
>
> for (i in 1:10){
>   set[i] <- complete(imp,i)}
>
> Yet that doesn't work, I also tried other things like:
> for (i in 1:10){
>   set[[i]] <- complete(imp,i)}
>
> Again, no success. It only saves a couple of lines of code, but there
must
> be an easy solution, right?
> Thanks,
> Florian

Hi Florian,

Please give more context. There are various reasons why that could
fail. Is your list "set" defined before starting to assign values?

Try:
set <- vector("list",10)
before the loop.



INSTITUTO DE BIOMECÁNICA DE VALENCIA
Universidad Politécnica de Valencia • Edificio 9C
Camino de Vera s/n • 46022 VALENCIA (ESPAÑA)
Tel. +34 96 387 91 60 • Fax +34 96 387 91 69
www.ibv.org

  Antes de imprimir este e-mail piense bien si es necesario hacerlo.
En cumplimiento de la Ley Orgánica 15/1999 reguladora de la Protección
de Datos de Carácter Personal, le informamos de que el presente mensaje
contiene información confidencial, siendo para uso exclusivo del
destinatario arriba indicado. En caso de no ser usted el destinatario
del mismo le informamos que su recepción no le autoriza a su divulgación
o reproducción por cualquier medio, debiendo destruirlo de inmediato,
rogándole lo notifique al remitente.

______________________________________________
[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
|

Re: Loop

Florian Weiler
Thanks for the answer, and sorry if I was not clear.

So I run the data imputation using mice with 10 chains and then I get a mids-object. From that object I can then extract 10 data sets using the complete(imp, n) command (with n=c(1:10)).

Now I can type this out 10 times:
set1 <- complete(imp, 1)
set2 <- complete(imp, 2)
etc.

Each of these set1 to set10 will be a data frame. That all is easy enough, but I would like to do this in a loop and create the same 10 data sets.

If I use the command proposed by you, then the loop works but again the 10 data sets are stored in only one single object (a list) when I would like to create 10 separate data sets.

Is that enough information?

Thanks again,
Florian
Reply | Threaded
Open this post in threaded view
|

Re: Loop

Berend Hasselman

On 22-02-2012, at 14:40, Florian Weiler wrote:

> Thanks for the answer, and sorry if I was not clear.
>
> So I run the data imputation using mice with 10 chains and then I get a
> mids-object. From that object I can then extract 10 data sets using the
> complete(imp, n) command (with n=c(1:10)).
>
> Now I can type this out 10 times:
> set1 <- complete(imp, 1)
> set2 <- complete(imp, 2)
> etc.
>
> Each of these set1 to set10 will be a data frame. That all is easy enough,
> but I would like to do this in a loop and create the same 10 data sets.
>
> If I use the command proposed by you, then the loop works but again the 10
> data sets are stored in only one single object (a list) when I would like to
> create 10 separate data sets.
>
> Is that enough information?

How about this

for( k in 1:10 )  assign(paste("set",k,sep=""), complete(imp,k))


Berend Hasselman

______________________________________________
[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
|

Re: Loop

Milan Bouchet-Valat
In reply to this post by Florian Weiler
Le mercredi 22 février 2012 à 05:40 -0800, Florian Weiler a écrit :

> Thanks for the answer, and sorry if I was not clear.
>
> So I run the data imputation using mice with 10 chains and then I get a
> mids-object. From that object I can then extract 10 data sets using the
> complete(imp, n) command (with n=c(1:10)).
>
> Now I can type this out 10 times:
> set1 <- complete(imp, 1)
> set2 <- complete(imp, 2)
> etc.
>
> Each of these set1 to set10 will be a data frame. That all is easy enough,
> but I would like to do this in a loop and create the same 10 data sets.
>
> If I use the command proposed by you, then the loop works but again the 10
> data sets are stored in only one single object (a list) when I would like to
> create 10 separate data sets.
With the suggested solution, you get a list of data sets. You can access
each data set separately as if it was a separate object, e.g. using
set[[1]]. It's usually more practical to have all objects in a list,
this allows you to run actions on all of them easily using lapply() and
things like that.

Hope this helps

______________________________________________
[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
|

Re: Loop

Florian Weiler
In reply to this post by Berend Hasselman
Thanks a lot all of you!

@ Berend, your code works fine, thanks.

@ Milan, you have a point there, makes sense to create 1 instead of 10 objects!
Reply | Threaded
Open this post in threaded view
|

Re: Loop

Eik Vettorazzi-2
In reply to this post by Florian Weiler
Hi Florian,

'yet that doesn't work' is an improper question on this list, see the
posting guide.
Besides that, something like
set<-vector(mode = "list", length = 10)
for (i in 1:10){
   set[[i]] <- complete(imp,i)}

or saving some typing

set<-lapply(1:10,function(i)complete(imp,i))

should work.

cheers

Am 22.02.2012 11:32, schrieb Florian Weiler:

> Dear all,
>
> I have a (probably very basic) question. I am imputing data with the mice
> package, using 10 chains. I can then write out the 10 final values of the
> chains simply by
>
> name1 <- complete(imp, 1)
>                       :
>                       :
> name10 <- complete(imp,10)
>
> Not a big deal, I just wanted to do that in a little loop as follows:
>
> for (i in 1:10){
>   set[i] <- complete(imp,i)}
>
> Yet that doesn't work, I also tried other things like:
> for (i in 1:10){
>   set[[i]] <- complete(imp,i)}
>
> Again, no success. It only saves a couple of lines of code, but there must
> be an easy solution, right?
> Thanks,
> Florian
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Loop-tp4409865p4409865.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.


--
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790

--
Pflichtangaben gemäß Gesetz über elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG):

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg

Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden), Dr. Alexander Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus

______________________________________________
[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.