Skip to content

Commit

Permalink
add contributor
Browse files Browse the repository at this point in the history
  • Loading branch information
DominiqueMakowski committed Aug 24, 2018
1 parent 707e319 commit 9ee5762
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ Authors@R: c(
email = "[email protected]",
role = c("aut", "cre", "cph"),
comment = c(ORCID = "0000-0001-5375-9967")),
person("Hugo",
"Najberg",
role = "ctb"),
person("Viliam",
"Simko",
role = "ctb",
email = "[email protected]"),
role = "ctb"),
person("Sasha",
"Epskamp",
role = "rev",
Expand Down
47 changes: 33 additions & 14 deletions R/simulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,56 @@
#'
#' Simulates data for single or multiple regression.
#'
#' @param sample Desired sample size.
#' @param coefs Desired theorethical coefs. Can be a single value or a list.
#' @param sample Desired sample size.
#' @param error The error (standard deviation of gaussian noise).
#'
#' @examples
#' library(psycho)
#'
#' data <- simulate_data_regression(sample=50, coefs=c(0, 0.5), error=0.1)
#' lm(y ~ ., data=data)
#' data <- simulate_data_regression(coefs=c(0, 0.8), sample=50, error=0)
#' fit <- lm(y ~ ., data=data)
#' coef(fit)
#' analyze(fit)
#'
#' @details See https://stats.stackexchange.com/questions/59062/multiple-linear-regression-simulation
#'
#' @author TPArrow
#'
#' @export
simulate_data_regression <- function(sample=10, coefs=0, error=0){
simulate_data_regression <- function(coefs=0.5, sample=100, error=0){

# Prevent error
coefs[coefs == 0] <- 0.01

y <- rnorm(sample, 0, 1)

n_var <- length(coefs)
X <- scale(matrix(rnorm(sample*(n_var), 0, 1), ncol=n_var))
X <- cbind(y, X)

n_var = length(coefs)
X = matrix(0, ncol=n_var, nrow=sample)
# find the current correlation matrix
cor_0 <- var(X)

beta = as.matrix(coefs)
# cholesky decomposition to get independence
chol_0 <- solve(chol(cor_0))

for (i in 1:n_var){
X[,i] = rnorm(sample, 0, 1)
}
X <- X %*% chol_0

y = X %*% beta + rnorm(sample, 0, error)
data = data.frame(X=X)
# create new correlation structure (zeros can be replaced with other r vals)
coefs_structure <- diag(x = 1, nrow=n_var+1, ncol=n_var+1, names = TRUE)
coefs_structure[-1, 1] <- coefs
coefs_structure[1, -1] <- coefs

X <- X %*% chol(coefs_structure) * sd(y) + mean(y)
X <- X[,-1]

# Add noise
y <- y + rnorm(sample, 0, error)

data <- data.frame(X)
names(data) <- paste0("V", 1:n_var)
data$y <- y
data$y <- as.vector(y)

return(data)
}

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h1 class="page-heading">About</h1>

<h1 class="page-heading">People</h1>

<b>Current <a href="https://github.com/neuropsychology/psycho.R/graphs/contributors">contributors</a> are <a href="https://dominiquemakowski.github.io/">D. Makowski</a>, <a href="https://github.com/vsimko">V. Simko</a>, and <a href="https://github.com/neuropsychology/psycho.R/blob/master/.github/CONTRIBUTING.md">YOU?</a></b>
<b>Current <a href="https://github.com/neuropsychology/psycho.R/graphs/contributors">contributors</a> are <a href="https://dominiquemakowski.github.io/">D. Makowski</a>, <a href="https://github.com/HugoNjb">H. Najberg</a>, <a href="https://github.com/vsimko">V. Simko</a>, and <a href="https://github.com/neuropsychology/psycho.R/blob/master/.github/CONTRIBUTING.md">YOU?</a></b>

<h1 class="page-heading">Posts</h1>

Expand Down
12 changes: 7 additions & 5 deletions man/simulate_data_regression.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9ee5762

Please sign in to comment.