View Full Version : Simple R code Questions


beji19308
04-12-2008, 10:58 PM
I am facing some problem with R codes.

I need to do a simulation study. So to simulate the data, i used the following inputs before i calculate the expect value of my estimations:

n=20
lamda = c(0.2, 0.5, 1, 2, 5)
u=rpois(n,lamda)
x_bar=mean(u)
nsim=10000

However i think there's some problem with this part. I can't seem to be able to properly define the R code to include 10000 samples of n=20 for each value of lamda.

beji19308
04-13-2008, 12:07 AM
Would anyone be able to enlighten me on how to count the number of certain observations in a sample?

In particular, i'm looking at how to count the number of zeros.

Much appreciate your time in reading this thread and posting replies to help me go further.

mp83
04-13-2008, 02:26 PM
I think the following might helpp you,Check it though, coz i wrote it in a hurry

n=20
zeros=matrix(NA,10000,5)
ll=c(0.2, 0.5, 1, 2, 5)


for (j in 1:10000)
{
for (i in 1:5)
{
temp<-rpois(n,ll[i])
zeros[j,i]<-length(temp[temp==0])
}
}

ram
05-09-2008, 09:39 AM
Would anyone be able to enlighten me on how to count the number of certain observations in a sample?

In particular, i'm looking at how to count the number of zeros.

Much appreciate your time in reading this thread and posting replies to help me go further.

If you have a vector of data and want to count the number of times a given number occurs then one simple method would be to measure the length of a subset of your vector. As an example:

my.data = c(0, 2, 5, 3, 1, 2, 3, 0, 0, 7, 3) # Define your data vector
my.data.zeros = my.data[my.data == 0] # Restrict the data to only thoses elements that are equal to zero
length(my.data.zeros) # Print how many elements this new vector has

There are probably much better ways to do this and/or functions written by other people! :)

Rounds
05-10-2008, 02:32 AM
applys are the trick to writing this efficiently. That for loop stuff above works and its the way I went about this stuff initially because I had a huge amount of C,C++, and Java experience. But the "R way" is with "apply" and various versions of it. Takes a little practice to think that way though.This works


n=20
lamda = c(0.2, 0.5, 1, 2, 5)
simulation = NULL # just creates an object that we will use to create list elements in. It is tidy to do it this way

simulation$lamda = rep(lamda,10000)
set.seed(2008)
simulation$result = sapply(simulation$lamda,rpois,n=n)
simulation$mean = apply(simulation$result,2, mean)

simulation$mean is now 50000 means for 10000 random samples from 5 different poisson.

To access means or results for each poisson make a mask like
mask = simulation$lamda == 0.5
simulation$result[,mask] # returns results for lamda .5
simulation$mean[mask] #returns samples means for lamda .5

PS nobody makes the claim ad hoc R style is memory efficient =)~ In alot of ways the language blows chunks imo.

edit: added set.seed(2008). set.seed causes the random number generator to start at a spot in its sequence specified by the set.seed number. It is essential for recreatability.

mp83
05-10-2008, 09:37 AM
Yeap. the xapply style is much better and elegant.Not a great user of them,myself,but I thought the for would be more instructive of what is going on,and I think the length(temp[temp==0]) is something worth seeing when using R