#' gen_response_surf_y: Generate DV under response surface model
#'
#' @param x_data IV data typically generated by gen_response_surf_x()
#' @param beta Vector of regression weights in the order of x_data. If generated by gen_response_surf_x(), this order is X1, X2, X1^2, X2^2, X1*X2.
#' @param sigma Error variance of outcome variable (optional). If NULL taken to be 0.
#' @param y_name name of y variable (optional).
#'
#' @return data frame with x and y variables given simulation parameters.
#' @export
#' @importFrom magrittr %>%
#'
#' @examples
#' # Importing for magrittr pipe (%>%)
#' library(tidyverse)
#' # Defining Correlation Matrix describing how x1 and x2 are related
#' # Covarince and variance of x1^2, x2^2, and x1*x2 follow from this matrix
#' cov_mat<-matrix(c(1, 0,
#' 0, 1), byrow = TRUE, 2, 2)
#'
#' # Defining betas x1, x2, x1^2, x2^2, and x1*x2
#' beta<-c(0, 0, -.075, -.075, .15)
#'
#' # Simulating 10,000 draws of size 1000 assuming the correlation structure and regression weights defined above.
#' sig_hat <- find_sig(n = 1000, cov_mat = cov_mat, beta = beta, target_var_y = 1)
#'
#' # Generating data frame for response suface examining leaders and follower agreeableness
#' simmed_df<-gen_response_surf_x(1000, cov_mat, x_names = c("L_Agree", "F_Agree"))%>%
#' gen_response_surf_y(beta = beta, sigma = sig_hat, y_name = "Satisfaction")
#'
#' head(simmed_df)
#'
#' # Variance of simmed_df approximates target_var_y (defined in find_sig above)
#' # Population level variance will be even closer
#' var(simmed_df$Satisfaction)
gen_response_surf_y<-function(x_data, beta, sigma=NULL, y_name=NULL){
if(is.null(y_name)){
y_name<-"y"
}
if(is.null(sigma)){
eps<-0
}else{
eps<-rnorm(nrow(x_data), 0, sigma)
}
x_data[y_name]<-as.vector(as.matrix(x_data)%*%beta+eps)
x_data
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.