Thank you Rui, this is indeed much simpler than anything I had thought of 
trying, and works perfectly.



________________________________
From: Rui Barradas [via R] [ml-node+s789695n4634712...@n4.nabble.com]
Sent: 28 June 2012 09:44
To: Natalie Franklin
Subject: Re: Replacing sets of rows in matrix within a loop

Hello,

You are replacing the values of runif each time through the loop. And
it's not just that, every time through, you are setting outcome[, 1] and
outcome[, 2] to the same values. Simply put, the loop is not needed.
Corrected:



n <- 100 ## patients per trial
trials <- 3 ## 3 trials
med <- as.matrix(c(2,4,3),)   ## for example

set.seed(123)

outcome <- matrix(nrow=n*trials, ncol=3)
outcome[, 1] <- rep(1:trials, each=n)
outcome[, 2] <- rep(1:n, trials)
outcome[, 3] <- runif(n*trials, 0, rep(med, each=n))

boxplot(outcome[, 3]~outcome[, 1])  # to see what we have


As you can see from the graph, the medians are now half of the values in
'med'. The name of this variable is misleading, it should be 'maxv' for
maximum value.
I've changed the name of 'median' to 'med', it already is an R function
name. It's misleading for a second reason.


Note also that there is another way of making 'trials' and 'n' vary. It
would change the columns order.

outcome <- expand.grid(1:n, 1:trials) # one instruction.
outcome <- cbind(outcome, runif(n*trials, 0, rep(med, each=n)))
colnames(outcome) <- c("patient", "trial", "value")

Simpler, no?

Hope this helps,

Rui Barradas

Em 27-06-2012 19:01, nqf escreveu:

> Dear R-help,
>
> I am writing some simulation code to create multiple sets of time-to-event
> clinical trial data (for use in meta-analysis). Within each trial, I want to
> apply censoring via simulation of uniform variables (with minimum zero and
> maximum the median outcome time for that particular trial).
>
> I have started by pre-allocating a matrix which has 3 columns; one for trial
> number, one for patient number, and I want to complete the third with the
> results of the uniform simulation.
>
> For example, I have :
> n<-10 ## patients per trial
> trials<-3 ## 3 trials
> outcome<-matrix(NaN,nrow=n*trials,ncol=3)
>   for(i in 1:trials){
>   outcome[,1]<-rep(1:trials,each=n)
>   outcome[,2]<-rep(1:n)
>   }
>
> In the third column, for each trial I want to create n sets of random draws:
> runif(n,0,median[i]), where median[i] is the median outcome time in that
> trial.
> median<-as.matrix(c(2,4,3),)   ## for example
>
> So far I have been trying to include the following within the for loop:
> outcome[,3]<-runif(n,0,median[i])
> But this just repeats the uniform draws from the last iteration of the loop
> in column 3.
>
> Can anyone advise how to replace the third column at each iteration rather
> than repeating? I think I can use rbind, but will this slow down my
> simulation?
>
> Many thanks in advance for your help.
>
> Natalie
>
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/Replacing-sets-of-rows-in-matrix-within-a-loop-tp4634658.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email]<https://db3prd0104.outlook.com/owa/UrlBlockedError.aspx> 
> 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]<https://db3prd0104.outlook.com/owa/UrlBlockedError.aspx> 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.


________________________________
If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/Replacing-sets-of-rows-in-matrix-within-a-loop-tp4634658p4634712.html
To unsubscribe from Replacing sets of rows in matrix within a loop, click 
here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4634658&code=bi5qLmZyYW5rbGluQHBnci5yZWFkaW5nLmFjLnVrfDQ2MzQ2NTh8LTEyMjU1MzM4NzY=>.
NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>


--
View this message in context: 
http://r.789695.n4.nabble.com/Replacing-sets-of-rows-in-matrix-within-a-loop-tp4634658p4634856.html
Sent from the R help mailing list archive at Nabble.com.
        [[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.

Reply via email to