Dear R experts, I am trying to simulate correlated binary data and have stumbled upon the following problem: With the help of "binarySimCLF" or "mvpBinaryEp" I have been able to simulate correlating binary vectors given certain mean values and a desired correlation. My problem is that these procedures do not allow you to specify the exact vector for which you want to generate a correlated vector. Is there anyway to do this? Maybe I can clarify my question by explaining what my goal is: I want to generate one Binary Vector (A), generate a correlated binary Vector (B), then generate a third binary Vector (C) that is correlated to B so that I can then see the occuring correlations between A and C. Thank you in advance, Marbles -- View this message in context: http://r.789695.n4.nabble.com/Simulate-binary-correlated-data-tp4660366.html Sent from the R help mailing list archive at Nabble.com.
On Tue, Mar 5, 2013 at 7:07 PM, Marbles <max-ihmels at gmx.de> wrote:> Dear R experts, > > I am trying to simulate correlated binary data and have stumbled upon the > following problem: > > With the help of "binarySimCLF" or "mvpBinaryEp" I have been able to > simulate correlating binary vectors given certain mean values and a desired > correlation. My problem is that these procedures do not allow you to specify > the exact vector for which you want to generate a correlated vector. Is > there anyway to do this? > > Maybe I can clarify my question by explaining what my goal is: > I want to generate one Binary Vector (A), generate a correlated binary > Vector (B), then generate a third binary Vector (C) that is correlated to B > so that I can then see the occuring correlations between A and C.IIRC, knowing the correlation between A & B and the correlation between B & C is not enough to uniquely specify the correlation between A & C (or perhaps even to bound it). Therefore i think your question is ill-defined. Though I might be wrong in the specific case of binary variates... Cheers, MW> > Thank you in advance, > Marbles > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Simulate-binary-correlated-data-tp4660366.html > Sent from the R help mailing list archive at Nabble.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.
Thank you for your response! I know that it is not enough to uniquely specify the correlation. That is why i would like to simulate it so that i can see how the resulting correlations between A and C are distributed in dependence of the correlations between A&B and B&C. -- View this message in context: http://r.789695.n4.nabble.com/Simulate-binary-correlated-data-tp4660366p4660380.html Sent from the R help mailing list archive at Nabble.com.
Btw: I have done this for non-binary variables already. If anyone is
interested, it looks like this.
I am pretty new to R, so excuse the potentially unelegant code.
# Implement library
library(ecodist)
# Prepare ACOutcome vector
ACOutcomes = c()
# Set desired variablelength & Number of simulations
VarLength = 1000
NSim = 10000
# Set desired range for correlations
rangeAB = c(0.0, 1.0)
rangeBC = c(0.0, 1.0)
# Start n simulation runs
n = 0
while(n < NSim) {n=n+1;
# Set desired correlations between Variables
DesCorAB = runif(1, rangeAB[1], rangeAB[2])
DesCorBC = runif(1, rangeBC[1], rangeBC[2])
# Simulate A and B with desired correlation between them
DatasetAB = corgen(len=VarLength, r=DesCorAB, epsilon=0.00)
A = DatasetAB$x
B = DatasetAB$y
# Option of saving correlation between A & B
cor(A, B, method = "pearson")
# Simulate C with desired correlation to B
C = corgen(x=B, r=DesCorBC, epsilon = 0.00)$y
# Calculate correlation between A & C
corAC = cor(A, C, method = "pearson")
# Save correlation AC into vector
ACOutcomes = append(ACOutcomes, corAC)
;}
# Show results: Mean, Minimum, Maximum
hist(ACOutcomes, breaks = 100)
mean(ACOutcomes)
min(ACOutcomes)
max(ACOutcomes)
--
View this message in context:
http://r.789695.n4.nabble.com/Simulate-binary-correlated-data-tp4660366p4660382.html
Sent from the R help mailing list archive at Nabble.com.
Reasonably Related Threads
- R code for var-cov matrix given variances and correlations
- Data separated by spaces, getting data into R using field lengths
- Generating uniformly distributed correlated data.
- How to generate a correlated binary data set?
- Generating an autocorrelated binary variable