TannerA
TannerA

Reputation: 5

How do I conduct an analysis by row ID where each row will be resampled (reanalyzed) 10 times before moving on to the next row in R?

I'm a beginner in R, and am working through data where I need to randomly sample from a von Mises distribution 10 times per row. I have already calculated the concentration parameter (kappa) of my data, and am using rvm() from the package CircStats to generate random samples. For each real observation, I have a von Mises mean (in degrees, "Example" column below):

Obs Example 1 1 69.43064 2 2 -41.80749 3 3 133.83900 4 4 -12.82486 5 5 -137.57358 6 6 -19.27882

Therefore if I were to calculate a random sample of 10 from a von Mises distribution with a concentration parameter (kappa) of .44, my code for the first observation would look like:

rvm(10,rad(69.43064),.44) [1] 0.7695183 5.9182905 2.6174674 5.6028430 2.4213428 5.4660423 6.1753582 [8] 2.6910969 4.2964024 5.4806146

I want to refer to the observed mean in the data, so:

rvm(10,rad(BearEx$Example), .44)

I'm looking to calculate 10 random values per observation. My ideal output would look like:

Obs Random 1 0.7695183 1 5.9182905 1 2.6174674 1 5.602843 1 2.4213428 1 5.4660423 1 6.1753582 1 2.6910969 1 4.2964024 1 5.4806146

And so on with each observation. I feel like this is a pretty basic problem, but I'm having trouble coding that loop with the observation number.

Thanks for your time!

Upvotes: 0

Views: 50

Answers (1)

kishan
kishan

Reputation: 40

If you have a data frame of the observations and their means ex:

obs_plus_mean = data.frame(obs = 1:5, mean = c(69.43064, -41.80749, 133.83900, -12.82486, -137.57358, -19.27882))

Then for a somewhat robust solution you can start by making a function that takes input of kappa, the sample mean and sample size n to generate a sample of size n for each observation. For convenience you can even put your default values for kappa and sample size.

von_mis_sample = function(mean, size = 10 , kappa = .44) { 
  sample = rvm(size,rad(mean),kappa)
}

Then you can compute your samples by the call

samples = sapply(obs_plus_mean$mean, von_mis_sample(mean))

(In your case I think you want BearEx$Example instead of obs_plus_mean$mean here)


That should work, please let me know otherwise.

Upvotes: 1

Related Questions