From 9ee576243e87dfde45589230d5970418234664aa Mon Sep 17 00:00:00 2001 From: DominiqueMakowski Date: Fri, 24 Aug 2018 12:59:52 +0200 Subject: [PATCH] add contributor --- DESCRIPTION | 6 +++-- R/simulate.R | 47 +++++++++++++++++++++++---------- docs/index.html | 2 +- man/simulate_data_regression.Rd | 12 +++++---- 4 files changed, 45 insertions(+), 22 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 54b2df9..c33b61f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -8,10 +8,12 @@ Authors@R: c( email = "dom.makowski@gmail.com", role = c("aut", "cre", "cph"), comment = c(ORCID = "0000-0001-5375-9967")), + person("Hugo", + "Najberg", + role = "ctb"), person("Viliam", "Simko", - role = "ctb", - email = "viliam.simko@gmail.com"), + role = "ctb"), person("Sasha", "Epskamp", role = "rev", diff --git a/R/simulate.R b/R/simulate.R index da951f7..ec479c9 100644 --- a/R/simulate.R +++ b/R/simulate.R @@ -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) } - diff --git a/docs/index.html b/docs/index.html index 07b52c5..35c45af 100644 --- a/docs/index.html +++ b/docs/index.html @@ -14,7 +14,7 @@

About

People

- Current contributors are D. Makowski, V. Simko, and YOU? + Current contributors are D. Makowski, H. Najberg, V. Simko, and YOU?

Posts

diff --git a/man/simulate_data_regression.Rd b/man/simulate_data_regression.Rd index 945e11e..de77372 100644 --- a/man/simulate_data_regression.Rd +++ b/man/simulate_data_regression.Rd @@ -4,13 +4,13 @@ \alias{simulate_data_regression} \title{Simulates data for single or multiple regression.} \usage{ -simulate_data_regression(sample = 10, coefs = 0, error = 0) +simulate_data_regression(coefs = 0.5, sample = 100, error = 0) } \arguments{ -\item{sample}{Desired sample size.} - \item{coefs}{Desired theorethical coefs. Can be a single value or a list.} +\item{sample}{Desired sample size.} + \item{error}{The error (standard deviation of gaussian noise).} } \description{ @@ -22,8 +22,10 @@ See https://stats.stackexchange.com/questions/59062/multiple-linear-regression-s \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) } \author{