Skip to content

Commit

Permalink
version 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
flh3 authored and gaborcsardi committed Feb 27, 2015
1 parent cb24136 commit 730bf2e
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 53 deletions.
11 changes: 5 additions & 6 deletions DESCRIPTION
@@ -1,14 +1,13 @@
Package: gendata
Type: Package
Title: Generate and modify synthetic datasets
Version: 1.0
Date: 2014-05-07
Title: Generate and Modify Synthetic Datasets
Version: 1.1
Date: 2015-02-27
Author: Francis Huang <flh3@hotmail.com>
Maintainer: Francis Huang <flh3@hotmail.com>
Description: Set of functions to create datasets using a correlation matrix.
License: GPL-3
Suggests: psych
Packaged: 2014-05-08 11:52:56 UTC; huangf
Packaged: 2015-03-01 00:27:32 UTC; huangf
NeedsCompilation: no
Repository: CRAN
Date/Publication: 2014-05-08 15:00:04
Date/Publication: 2015-03-01 08:20:17
14 changes: 7 additions & 7 deletions MD5
@@ -1,11 +1,11 @@
7195afc887c32881466e57af6b4ed44d *DESCRIPTION
d24062a9da5bf317849a4f9f55c7425b *DESCRIPTION
df390c53434517b304ac5db487184641 *NAMESPACE
342f1ce5c35aeb3cf31f40751e358665 *R/dtrans.R
f4f8cedfa2020bf348b967e67aba1b87 *R/genmvnorm.R
095100e3b3da1f45b3598b24015efb31 *R/genmvnorm.R
01ef3ab6e2a122c3c91d6870bc17a2e6 *R/recalib.R
4cbd5ff37108a8c4cd99a19946511f89 *R/revcode.R
67874b9ff91c1fd314102a5dc00cf566 *man/dtrans.Rd
7ad81a8b5b71270c0033a82d41998a13 *man/gendata-package.Rd
63f2bee019b17a527c52d9a5d84d6862 *man/genmvnorm.Rd
0e7981599f1f44e390fb28f645fde363 *man/recalib.Rd
72fe602011db9615eb043cb9a50193b6 *man/revcode.Rd
ee03b3f57121283e65077055b925d2e7 *man/dtrans.Rd
fba3faa2ba40f50b4a7edaa14e4320ff *man/gendata-package.Rd
2e045b7357aa5e2ed17cb27a065962a0 *man/genmvnorm.Rd
c1b548f7156dc9fcc7e51e5d55100240 *man/recalib.Rd
a0f1851ab48c58d59a1ec40b87589faa *man/revcode.Rd
77 changes: 46 additions & 31 deletions R/genmvnorm.R
@@ -1,35 +1,50 @@

####################
genmvnorm<-function(cor,k,n,seed=F){
if(require("psych")!=T){
print("installing 'psych' package")
install.packages("psych")
require(psych)}
if(seed != F){set.seed(seed)}
if(is.matrix(cor)==F){
x<-length(cor)
if(x != (k*(k-1)/2) ){stop("STOP: wrong correlation table")}
cr.cor<-matrix(NA,k,k)
diag(cr.cor)<-1
cr.cor[lower.tri(cr.cor)]<-cor
cr.cor[upper.tri(cr.cor)]<-t(cr.cor)[upper.tri(cr.cor)]
fit<-principal(cr.cor,rotate=F,nfactors=k)}

if(is.matrix(cor)==T){ #if a correlation matrix was used
fit<-principal(cor,rotate=F,nfactors=k)
}

l<-fit$loadings[1:k,1:k]
f<-matrix(nrow=k,l)
dim(f)
ma<-matrix(nrow=n,ncol=k)
for (i in 1:k){
ma[,i]<-rnorm(n)


if(is.matrix(cor)==F){
x<-length(cor)
if(x != (k*(k-1)/2) ){stop("STOP: wrong correlation table")}

cr.cor<-matrix(NA,k,k)
diag(cr.cor)<-1
cr.cor[lower.tri(cr.cor)]<-cor
cr.cor[upper.tri(cr.cor)]<-t(cr.cor)[upper.tri(cr.cor)]

e<-eigen(cr.cor)
L<-e$values #placing the eigenvalues in V
Vm<-matrix(0,nrow=k,ncol=k) #creating a k x k matrix.
diag(Vm)<-L #putting the eigenvalues on the diagonals
Vm #check-- matrix with eigenvalues
e$vectors #these are the eigenvectors
l<-e$vectors %*% sqrt(Vm) #these are the loadings
}

if(is.matrix(cor)==T){ #if a correlation matrix was used

e<-eigen(cor)
L<-e$values #placing the eigenvalues in V
Vm<-matrix(0,nrow=k,ncol=k) #creating a k x k matrix.
diag(Vm)<-L #putting the eigenvalues on the diagonals
Vm #check-- matrix with eigenvalues
e$vectors #these are the eigenvectors
l<-e$vectors %*% sqrt(Vm) #these are the loadings

}

if(seed != F){set.seed(seed)}
f<-matrix(nrow=k,l)
#dim(f)
ma<-matrix(nrow=n,ncol=k)
for (i in 1:k){
ma[,i]<-rnorm(n)
}
tma<-t(ma)
sol<-f %*% tma
sol<-t(sol)

data<-data.frame(sol)
return(data)

}
tma<-t(ma)
sol<-f %*% tma
sol<-t(sol)

data<-data.frame(sol)
return(data)
}
2 changes: 1 addition & 1 deletion man/dtrans.Rd
Expand Up @@ -2,7 +2,7 @@
\alias{dtrans}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{
Data transform
Data Transform
}
\description{
Transforms variables in a dataset with a specified mean and standard deviation.
Expand Down
8 changes: 4 additions & 4 deletions man/gendata-package.Rd
Expand Up @@ -3,7 +3,7 @@
\alias{gendata}
\docType{package}
\title{
Generate synthetic datasets
Generate Synthetic Datasets
}
\description{
Create synthetic datasets based on a correlation table. Additional functions can be used to rescale, transform, and reverse code variables.
Expand All @@ -12,8 +12,8 @@ Create synthetic datasets based on a correlation table. Additional functions can
\tabular{ll}{
Package: \tab gendata\cr
Type: \tab Package\cr
Version: \tab 1.0\cr
Date: \tab 2014-05-07\cr
Version: \tab 1.1\cr
Date: \tab 2012-02-27\cr
License: \tab GPL-3\cr
}

Expand All @@ -33,7 +33,7 @@ Maintainer: Francis Huang <flh3@hotmail.com>
}
\references{

Fan, X., Sivo, S., & Keenan, S. (2002). SAS for Monte Carlo studies: A guide for quantitative researchers. Sas Institute.
Fan, X., Felsovalyi, A., Sivo, S., & Keenan, S. (2002). SAS for Monte Carlo studies: A guide for quantitative researchers. SAS Institute.
}

\keyword{ package }
Expand Down
4 changes: 2 additions & 2 deletions man/genmvnorm.Rd
Expand Up @@ -2,7 +2,7 @@
\alias{genmvnorm}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{
genmvnorm
Genmvnorm
}
\description{
Generates a multivariate normal dataset based on a specified correlation matrix.
Expand Down Expand Up @@ -32,7 +32,7 @@ For creating synthetic datasets. Based on the SAS chapter by Fan et al. (2002).

\references{Based on:

Fan, X., Sivo, S., & Keenan, S. (2002). SAS for Monte Carlo studies: A guide for quantitative researchers. SAS Institute.
Fan, X., Felsovalyi, A., Sivo, S., & Keenan, S. (2002). SAS for Monte Carlo studies: A guide for quantitative researchers. SAS Institute.
}
\author{
Francis Huang
Expand Down
2 changes: 1 addition & 1 deletion man/recalib.Rd
Expand Up @@ -2,7 +2,7 @@
\alias{recalib}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{
Recalibrate (rescale) variables
Recalibrate (rescale) Variables
}
\description{
Rescale variables (one at a time) to have a new minimum and maximum value.
Expand Down
2 changes: 1 addition & 1 deletion man/revcode.Rd
Expand Up @@ -2,7 +2,7 @@
\alias{revcode}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{
Reverse coding variables
Reverse Coding Variables
}
\description{
Reverse codes variables
Expand Down

0 comments on commit 730bf2e

Please sign in to comment.