Re: [R] Sampling from a Matrix

2006-08-10 Thread Daniel Gerlanc
Once again, thanks for your help.

I did not state the problem correctly, though the code is correct for
what I want to do.

A better description of the problem would be that there is a matrix of
probabilities:

 set.seed(1)
 probs - array(abs(rnorm(25, sd = 0.33)), dim = c(5,5), dimnames = list(1:5, 
 letters[1:5]))
 probs
   a  b   c de
1 0.21 0.27 0.50 0.0148 0.303
2 0.06 0.16 0.13 0.0053 0.258
3 0.28 0.24 0.21 0.3115 0.025
4 0.53 0.19 0.73 0.2710 0.656
5 0.11 0.10 0.37 0.1960 0.205

The column names, dimnames(probs)[[2]], are the names of units to be
sampled.Each row is a trial.  For each row (trial), I want to
sample 3 of the units such that for each row I get a vector like the
following:

[1] a, b, a

The samples are to be done with replacement, and these vectors could
be combined to form a matrix of the samples.

The purpose of the probs matrix is to give each unit a probability
that it will be sampled.

One way to do this follows:

index - 1:ncol(probs)

res - matrix(0,
  nrow = dim(probs)[1],
  ncol = 3
)

for(i in 1:nrow(probs)){

## gets the indexes of the values chosen

res[, i] - sample(index, size = 3, replace = TRUE, prob = probs[i, ])

}

Using apply as Andy described would accomplish the intended result.

-- Dan

  Hmm... If I read Daniel's code (which is different from his description)
  correctly, that doesn't seem to be what he wanted.  Perhaps something like
  this:
 
  apply(probs, 1, function(p) sample(1:ncol(probs), 3, replace=TRUE, prob=p))
 
  Andy

 Andy,

 You are of course correct. I had focused on the description of the
 problem, rather than the code provided, presuming that the code was not
 correct, including the use of 'replace' and 'prob' in sample().

 I suppose it would be up to Daniel for clarification.

 Regards,

 Marc





-- 
Daniel Gerlanc
Williams College '07

__
R-help@stat.math.ethz.ch 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] Sampling from a Matrix

2006-08-09 Thread Marc Schwartz (via MN)
On Tue, 2006-08-08 at 14:10 -0400, Liaw, Andy wrote:
 From: Marc Schwartz
  
  On Fri, 2006-08-04 at 12:46 -0400, Daniel Gerlanc wrote:
   Hello all,
   
   Consider the following problem:
   
   There is a matrix of probabilities:
   
set.seed(1)
probs - array(abs(rnorm(25, sd = 0.33)), dim = c(5,5), 
  dimnames = 
list(1:5, letters[1:5])) probs
   a  b   c de
   1 0.21 0.27 0.50 0.0148 0.303
   2 0.06 0.16 0.13 0.0053 0.258
   3 0.28 0.24 0.21 0.3115 0.025
   4 0.53 0.19 0.73 0.2710 0.656
   5 0.11 0.10 0.37 0.1960 0.205
   
   I want to sample 3 values from each row.
   
   One way to do this follows:
   
   index - 1:ncol(probs)
   
   for(i in 1:nrow(probs)){
   
   ## gets the indexes of the values chosen
   
   sample(index, size = 3, replace = TRUE, prob = probs[i, ])
   
   }
   
   Is there a another way to do this?
   
   Thanks!
  
   t(apply(probs, 1, function(x) sample(x, 3)))
 [,1]   [,2]   [,3]
  1 0.210 0.5000 0.0148
  2 0.258 0.0053 0.1300
  3 0.025 0.2800 0.3115
  4 0.190 0.5300 0.2710
  5 0.196 0.1000 0.1100
 
 Hmm... If I read Daniel's code (which is different from his description)
 correctly, that doesn't seem to be what he wanted.  Perhaps something like
 this:
 
 apply(probs, 1, function(p) sample(1:ncol(probs), 3, replace=TRUE, prob=p))
 
 Andy

Andy,

You are of course correct. I had focused on the description of the
problem, rather than the code provided, presuming that the code was not
correct, including the use of 'replace' and 'prob' in sample().

I suppose it would be up to Daniel for clarification.

Regards,

Marc

__
R-help@stat.math.ethz.ch 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] Sampling from a Matrix

2006-08-08 Thread Liaw, Andy
From: Marc Schwartz
 
 On Fri, 2006-08-04 at 12:46 -0400, Daniel Gerlanc wrote:
  Hello all,
  
  Consider the following problem:
  
  There is a matrix of probabilities:
  
   set.seed(1)
   probs - array(abs(rnorm(25, sd = 0.33)), dim = c(5,5), 
 dimnames = 
   list(1:5, letters[1:5])) probs
  a  b   c de
  1 0.21 0.27 0.50 0.0148 0.303
  2 0.06 0.16 0.13 0.0053 0.258
  3 0.28 0.24 0.21 0.3115 0.025
  4 0.53 0.19 0.73 0.2710 0.656
  5 0.11 0.10 0.37 0.1960 0.205
  
  I want to sample 3 values from each row.
  
  One way to do this follows:
  
  index - 1:ncol(probs)
  
  for(i in 1:nrow(probs)){
  
  ## gets the indexes of the values chosen
  
  sample(index, size = 3, replace = TRUE, prob = probs[i, ])
  
  }
  
  Is there a another way to do this?
  
  Thanks!
 
  t(apply(probs, 1, function(x) sample(x, 3)))
[,1]   [,2]   [,3]
 1 0.210 0.5000 0.0148
 2 0.258 0.0053 0.1300
 3 0.025 0.2800 0.3115
 4 0.190 0.5300 0.2710
 5 0.196 0.1000 0.1100

Hmm... If I read Daniel's code (which is different from his description)
correctly, that doesn't seem to be what he wanted.  Perhaps something like
this:

apply(probs, 1, function(p) sample(1:ncol(probs), 3, replace=TRUE, prob=p))

Andy

 
 See ?apply and ?t
 
 HTH,
 
 Marc Schwartz

__
R-help@stat.math.ethz.ch 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] Sampling from a Matrix

2006-08-04 Thread Marc Schwartz (via MN)
On Fri, 2006-08-04 at 12:46 -0400, Daniel Gerlanc wrote:
 Hello all,
 
 Consider the following problem:
 
 There is a matrix of probabilities:
 
  set.seed(1)
  probs - array(abs(rnorm(25, sd = 0.33)), dim = c(5,5), dimnames = 
  list(1:5, letters[1:5]))
  probs
 a  b   c de
 1 0.21 0.27 0.50 0.0148 0.303
 2 0.06 0.16 0.13 0.0053 0.258
 3 0.28 0.24 0.21 0.3115 0.025
 4 0.53 0.19 0.73 0.2710 0.656
 5 0.11 0.10 0.37 0.1960 0.205
 
 I want to sample 3 values from each row.
 
 One way to do this follows:
 
 index - 1:ncol(probs)
 
 for(i in 1:nrow(probs)){
 
 ## gets the indexes of the values chosen
 
 sample(index, size = 3, replace = TRUE, prob = probs[i, ])
 
 }
 
 Is there a another way to do this?
 
 Thanks!

 t(apply(probs, 1, function(x) sample(x, 3)))
   [,1]   [,2]   [,3]
1 0.210 0.5000 0.0148
2 0.258 0.0053 0.1300
3 0.025 0.2800 0.3115
4 0.190 0.5300 0.2710
5 0.196 0.1000 0.1100

See ?apply and ?t

HTH,

Marc Schwartz

__
R-help@stat.math.ethz.ch 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.