Skip to content

Commit

Permalink
version 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
flh3 authored and cran-robot committed May 9, 2022
1 parent 730bf2e commit 97bab41
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 85 deletions.
8 changes: 4 additions & 4 deletions DESCRIPTION
@@ -1,13 +1,13 @@
Package: gendata
Type: Package
Title: Generate and Modify Synthetic Datasets
Version: 1.1
Date: 2015-02-27
Version: 1.2.0
Date: 2022-05-09
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
Packaged: 2015-03-01 00:27:32 UTC; huangf
NeedsCompilation: no
Packaged: 2022-05-09 21:40:21 UTC; flh3
Repository: CRAN
Date/Publication: 2015-03-01 08:20:17
Date/Publication: 2022-05-09 21:50:02 UTC
20 changes: 11 additions & 9 deletions MD5
@@ -1,11 +1,13 @@
d24062a9da5bf317849a4f9f55c7425b *DESCRIPTION
df390c53434517b304ac5db487184641 *NAMESPACE
342f1ce5c35aeb3cf31f40751e358665 *R/dtrans.R
095100e3b3da1f45b3598b24015efb31 *R/genmvnorm.R
01ef3ab6e2a122c3c91d6870bc17a2e6 *R/recalib.R
4cbd5ff37108a8c4cd99a19946511f89 *R/revcode.R
ee03b3f57121283e65077055b925d2e7 *man/dtrans.Rd
0d518d37b4fcc76ec1c6fb54d1154925 *DESCRIPTION
901331ca84ce9a4c48f13b6f3c345713 *NAMESPACE
030758d4fc185b2e3dbd0eec5ae9f704 *NEWS.md
c0205abf0548202c060f77fa82e712d7 *R/dtrans.R
be085e3c9d0854c746af9ccff06bca44 *R/genmvnorm.R
9b91c42e2e1b13213efd59e72a597bf8 *R/recalib.R
94e1cc2fad3a571b2bafca401160171d *R/revcode.R
f8b23867f24b2e0d1e66038f48228cc8 *README.md
44d1f5b47fd8044323ad2b44f8e91375 *man/dtrans.Rd
fba3faa2ba40f50b4a7edaa14e4320ff *man/gendata-package.Rd
2e045b7357aa5e2ed17cb27a065962a0 *man/genmvnorm.Rd
c1b548f7156dc9fcc7e51e5d55100240 *man/recalib.Rd
9214c09fde33d3ec4e7842541fb05ff0 *man/genmvnorm.Rd
f95f68cdd0f0073fc7c8a7aa9101b65d *man/recalib.Rd
a0f1851ab48c58d59a1ec40b87589faa *man/revcode.Rd
1 change: 1 addition & 0 deletions NAMESPACE
@@ -1 +1,2 @@
exportPattern("^[[:alpha:]]+")
importFrom("stats", "rnorm")
10 changes: 10 additions & 0 deletions NEWS.md
@@ -0,0 +1,10 @@
# 2022.05.09 v. 1.2
- Adjusted spacing in function to make it more readable.
- Updated package to use (based on e-mail from CRAN):

Consider adding
importFrom("stats", "rnorm")
to your NAMESPACE file.

# 2015-02-27 v. 1.1
- minor update
27 changes: 14 additions & 13 deletions R/dtrans.R
@@ -1,21 +1,22 @@
#transform dataset to specified m and sd
dtrans<-function(data,m,sd,rnd=F){
dtrans <- function(data, m, sd, rnd = FALSE){
#basic checking
x<-dim(data)[2] #getting number of vars
cat("Number of variables in dataset:", x,"\n")#number of vars
cat("Number of means specified:", length(m),"\n")
cat("Number of standard deviations in dataset:", length(sd),"\n")
if(length(m) != length(sd)){stop("Number of means/SDs should match.")}
if(length(m) != dim(data)[2]){stop("Incorrect number of means/SDs")}
for (i in 1:x){
xrange<-range(data[,1])
if(xrange[1]-xrange[2]==0){stop("Constant value detected")}
x <- dim(data)[2] #getting number of vars
cat("Number of variables in dataset:", x,"\n")#number of vars
cat("Number of means specified:", length(m),"\n")
cat("Number of standard deviations in dataset:", length(sd),"\n")
if(length(m) != length(sd)){stop("Number of means/SDs should match.")}
if(length(m) != dim(data)[2]){stop("Incorrect number of means/SDs")}
for (i in 1:x){
xrange <- range(data[,1])
if(xrange[1] - xrange[2] == 0){stop("Constant value detected")}
}

#transforming
for (i in 1:x){
data[i]=m[i]+sd[i]*data[i]
data[i] = m[i] + sd[i] * data[i]
}
if(rnd==T){return(round(data,0))
} else {return(data)}
if(rnd == TRUE){return(round(data, 0))
} else {
return(data)}
}
54 changes: 27 additions & 27 deletions R/genmvnorm.R
@@ -1,49 +1,49 @@

genmvnorm<-function(cor,k,n,seed=F){
genmvnorm <- function(cor, k, n, seed = FALSE){


if(is.matrix(cor)==F){
x<-length(cor)
if(x != (k*(k-1)/2) ){stop("STOP: wrong correlation table")}
if(is.matrix(cor) == FALSE){
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)]
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
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
l <- e$vectors %*% sqrt(Vm) #these are the loadings
}

if(is.matrix(cor)==T){ #if a correlation matrix was used
if(is.matrix(cor) == TRUE){ #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
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
l <- e$vectors %*% sqrt(Vm) #these are the loadings

}

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

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

}
Expand Down
18 changes: 9 additions & 9 deletions R/recalib.R
@@ -1,13 +1,13 @@
recalib<-function(data,var,low,high){
nmin<-low
nmax<-high
if(low>high){
nmax<-low
nmin<-high
recalib<-function(data, var, low, high){
nmin <- low
nmax <- high
if(low > high){
nmax <- low
nmin <- high
}
cmin<-min(data[,var])
cmax<-max(data[,var])
cmin <- min(data[,var])
cmax <- max(data[,var])

data[,var]<-(nmax-nmin)/(cmax-cmin)*(data[,var]-cmin)+nmin
data[,var] <- (nmax - nmin) / (cmax - cmin) * (data[,var] - cmin) + nmin
return(data)
}
10 changes: 5 additions & 5 deletions R/revcode.R
@@ -1,10 +1,10 @@
#reverse coding
revcode<-function(data,vars){
x<-length(vars)
revcode <- function(data, vars){
x <- length(vars)
for (i in 1:x){
mx<-max(data[,vars[i]])
mn<-min(data[,vars[i]])
data[,vars[i]]<-(mx)-data[,vars[i]]+(mn)
mx <- max(data[,vars[i]])
mn <- min(data[,vars[i]])
data[,vars[i]] <- (mx) - data[,vars[i]] + (mn)
}
return(data)
}
2 changes: 2 additions & 0 deletions README.md
@@ -0,0 +1,2 @@
# gendata

21 changes: 10 additions & 11 deletions man/dtrans.Rd
Expand Up @@ -8,21 +8,21 @@ Data Transform
Transforms variables in a dataset with a specified mean and standard deviation.
}
\usage{
dtrans(data, m, sd, rnd = F)
dtrans(data, m, sd, rnd = FALSE)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
\item{data}{
name of your dataset
name of your dataset.
}
\item{m}{
indicate a vector of desired means
indicate a vector of desired means.
}
\item{sd}{
indicate a vector of desired standard deviations
indicate a vector of desired standard deviations.
}
\item{rnd}{
Indicates if you want to round the numbers (no decimals). T or F.
indicates if you want to round the numbers (no decimals). \code{TRUE} or \code{FALSE}.
}
}
\author{
Expand All @@ -31,23 +31,22 @@ Francis Huang

\examples{

sdata<-genmvnorm(cor=c(.7,.2,.3),k=3,n=500,seed=12345)
sdata <- genmvnorm(cor = c(.7, .2, .3), k = 3, n = 500, seed = 12345)
cor(sdata)
summary(sdata)
#note: data are in z scores

s2<-dtrans(sdata,c(0,100,50),c(1,15,10),rnd=FALSE)
s2 <- dtrans(sdata, c(0, 100, 50), c(1, 15, 10), rnd = FALSE)
summary(s2)
sd(s2[,2])
sd(s2[,3])
#note: variables X2 and X3 are now rescaled with the appropriate means and standard deviations.
head(s2)

s2<-dtrans(sdata,c(0,100,50),c(1,15,10),rnd=TRUE)
#at times, you may want a dataset to not have decimals. use rnd=T.
s2 <- dtrans(sdata, c(0, 100, 50), c(1, 15, 10), rnd = TRUE)
#at times, you may want a dataset to not have decimals. use \code{rnd= TRUE}.
head(s2)
}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
\keyword{ transform }
\keyword{ data }% __ONLY ONE__ keyword per line
\keyword{transform}
2 changes: 1 addition & 1 deletion man/genmvnorm.Rd
Expand Up @@ -8,7 +8,7 @@ Genmvnorm
Generates a multivariate normal dataset based on a specified correlation matrix.
}
\usage{
genmvnorm(cor, k, n, seed = F)
genmvnorm(cor, k, n, seed = FALSE)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
Expand Down
9 changes: 3 additions & 6 deletions man/recalib.Rd
Expand Up @@ -39,20 +39,17 @@ Francis Huang
\link[gendata:dtrans]{dtrans}
}
\examples{
sdata<-genmvnorm(cor=c(.7,.2,.3),k=3,n=500,seed=12345)
sdata <- genmvnorm(cor = c(.7, .2, .3), k = 3, n = 500, seed = 12345)
cor(sdata)
summary(sdata[,1])
#note the min and max of variable X1
#changes variable one to have a minimum of 10 and a maximum of 50
#correlations remain the same

s2<-recalib(sdata,1,10,50)
s2 <- recalib(sdata, 1, 10, 50)
cor(s2)
summary(s2[,1])
#note revised values of variable X1

}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
\keyword{ ~kwd1 }
\keyword{ ~kwd2 }% __ONLY ONE__ keyword per line

0 comments on commit 97bab41

Please sign in to comment.