[R] Custom sampling method in R XXXX

Daniel Nordlund djnordlund at frontier.com
Tue Jun 24 02:23:16 CEST 2014


Something like this could work


x <- 0.250
new_sample <- function(xx) {
  j<-c(0.000,0.125,0.250,0.375,0.500,0.625,0.750,0.875,1.000)
  probs<-c(0.02307692,0.20769231,0.53846154,0.20769231,0.02307692)
  jj <- c(0,0,j,1,1)
  ndx <- which(j == xx)
  sample(jj[ndx:(ndx+4)], size=1, p=probs, replace=TRUE)
}
new_sample(x)



Daniel Nordlund
Bothell, WA USA
 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Dan Abner
> Sent: Monday, June 23, 2014 3:19 PM
> To: Greg Snow
> Cc: r-help at r-project.org
> Subject: Re: [R] Custom sampling method in R XXXX
> 
> Hi Greg,
> 
> Thanks, this makes sense. I can envision the call to the sample fn
> like you are discribing. Any ideas on how to construct the vector? I
> still am unclear about that.
> 
> Thanks,
> 
> Dan
> 
> On Mon, Jun 23, 2014 at 5:26 PM, Greg Snow <538280 at gmail.com> wrote:
> > The sample function can be used to sample discrete values with
> > designated probabilities.  I would just construct your list of 5
> > values based on the selected value (duplicating end values if needed,
> > so a choice of x=0 would be the vector c(0,0,0, 0.125, 0.25) ), then
> > sample from this vector with the probabilities that you specify.
> >
> > On Mon, Jun 23, 2014 at 3:11 PM, Dan Abner <dan.abner99 at gmail.com>
> wrote:
> >>  Hi all,
> >>
> >> I have the following situation and a good efficient way to perform
> >> this operation in R has not come to me. Any suggestions/input are
> >> welcome.
> >>
> >> I have a user-defined parameter (let's call it x) whose value is
> >> selected from a set of possible values (j). Once the user selects one
> >> of the values of j for x, then I need to map a probability
> >> distribution to the values of j such that the middle probability of
> >> .5385 (see probs below) is associated with the value of x and the tail
> >> probabilities are assigned to the 2 values below x and 2 values above
> >> x in j. Therefore, in the example below:
> >>
> >>
> >> x<-.250
> >> j<-c(0.000,0.125,0.250,0.375,0.500,0.625,0.750,0.875,1.000)
> >> probs<-c(0.02307692,0.20769231,0.53846154,0.20769231,0.02307692)
> >>
> >> probabilities would be assigned to the values of j as such:
> >>
> >> value     probability
> >> 0            0.023077
> >> 0.125     0.207692
> >> 0.25       0.538462
> >> 0.375     0.207692
> >> 0.5         0.023077
> >>
> >> And then 1 value of j is selected based on the associated probability.
> >> Any ideas on an efficient way to do this?
> >>
> >> An added dimension of complexity is when the value of x is selected
> >> near the parameter boundary of j. If x = 0, then the easiest thing I
> >> can think of is to assign probabilities as:
> >>
> >> value      probability
> >> 0             0.76923077
> >> 0.125      0.207692
> >> 0.25        0.023077
> >>
> >> However, I am open to other possibilities.
> >>
> >> Any assistance is appreciated.
> >>
> >> Thanks,
> >>
> >> Dan
> >>
> >> ______________________________________________
> >> R-help at 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.
> >
> >
> >
> > --
> > Gregory (Greg) L. Snow Ph.D.
> > 538280 at gmail.com
> 
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list