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.


[R] Vectorizing a for loop

2006-08-03 Thread Daniel Gerlanc
Hello all,

Consider the following problem:

There are two vectors:

rows - c(1, 2, 3, 4, 5)
columns - c(10, 11, 12, 13, 14)

I want to create a matrix with dimensions length(rows) x length(columns):

res - matrix(nrow = length(rows), ncol = length(columns))

If i and j are the row and column indexes respectively, the values
of the cells are abs(rows[i] - columns[j]).  The resultant matrix
follows:

 [,1] [,2] [,3] [,4] [,5]
[1,]9   10   11   12   13
[2,]8910   11   12
[3,]78  9   10   11
[4,]67  8 9   10
[5,]56  7 89

This matrix may be generated by using a simple for loop:

for(i in 1:length(rows)){
  for(j in 1:length(columns)){
res[i,j] - abs(rows[i] - columns[j])
  }
}

Is there a quicker, vector-based approach for doing this or a function
included in the recommended packages that does this?

Thanks!

-- Dan Gerlanc
Williams College

__
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.


[R] How to split the left and right hand terms of a formula

2006-07-26 Thread Daniel Gerlanc
Hello All,

I've sent a few messages to the list regarding splitting a formula
into its right and left hand terms.  Thanks to everyone who has
responded.

I believe that the best way to extract the left and right hand terms
as character vectors follows:

library(nlme)

formula - y ~ x + z

left.term - all.vars(getResponseFormula(formula))
covariates - all.vars(getCovariateFormula(formula))

Thanks!

Dan Gerlanc
Williams College

__
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.


[R] Follow Up To: Splitting the left and right hand terms of a formula

2006-07-25 Thread Daniel Gerlanc
Hi All,

I sent the following message to R-help on July 14th, 2006:

Let's say I have the following formula:

a.formula - x ~ y + z

I want to extract the left and right-hand sides of the function so
that I have two character vectors like the ones you would create using
the following assignments:

left.hand.side - x
right.hand.side - c(y, z)

One way to do this follows:

left.hand.side - unlist(dimnames(attr(terms(a.formula), factors))[1])
right.hand.side - unlist(dimnames(attr(terms(a.formula), factors))[-1])

Is there a better or cleaner way to do this?

I got one reply to try this (thanks Gabor!):

 all.vars(update(a.formula, .~0))
[1] x

 all.vars(update(a.formula, 0~.))
[1] y z

This works, but seems a bit of a hack.

There are two methods, getCovariateFormula, and getCovariate,
which return, respectively, objects of class formula for the left
and right hand sides of the formula.  To get the right hand side or
left hand side I wrap one of these method calls in a call to
all.vars.  Is there a better way to get the left or right hand side
of the formula, or is this it?

Thanks!

Dan Gerlanc
Williams College

__
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.


[R] Splitting the left and right hand terms of a formula

2006-07-14 Thread Daniel Gerlanc
Let's say I have the following formula:

a.formula - x ~ y + z

I want to extract the left and right-hand sides of the function so
that I have two character vectors like the ones you would create using
the following assignments:

left.hand.side - x
right.hand.side - c(y, z)

One way to do this follows:

left.hand.side - unlist(dimnames(attr(terms(a.formula), factors))[1])
right.hand.side - unlist(dimnames(attr(terms(a.formula), factors))[-1])

Is there a better or cleaner way to do this?

Thanks!

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