diff --git a/.Rbuildignore b/.Rbuildignore new file mode 100644 index 0000000..1c92417 --- /dev/null +++ b/.Rbuildignore @@ -0,0 +1,5 @@ +^.*\.Rproj$ +^\.Rproj\.user$ +^README\.Rmd$ +^README-.*\.png$ +^\.travis\.yml$ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..56843bc --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata +src/*.o +src/*.so +src/*.dll diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8d139ac --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r + +language: R +sudo: false +cache: packages diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..0c41ff1 --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,20 @@ +Package: iccbeta +Type: Package +Title: Multilevel model intraclass correlation for slope heterogeneity +Version: 1.0 +Date: 2014-11-21 +Authors@R: c(person("Steven Andrew", "Culpepper", role = c("aut", "cph","cre"), email = + "sculpepp@illinois.edu"),person("Herman", "Aguinis", role = c("aut", "cph"), email = + "haguinis@indiana.edu")) +Maintainer: Steven Andrew Culpepper +Description: A function and vignettes for computing an intraclass correlation described in Aguinis & Culpepper (in press). iccbeta quantifies the share of variance in a dependent variable that is attributed to group heterogeneity in slopes. +License: GPL (>= 2) +Imports: Rcpp (>= 0.11.1) +LinkingTo: Rcpp (>= 0.11.1), RcppArmadillo +Depends: R (>= 3.0.2), lme4 +Packaged: 2014-11-25 02:21:38 UTC; sculpepp +Author: Steven Andrew Culpepper [aut, cph, cre], + Herman Aguinis [aut, cph] +NeedsCompilation: yes +Repository: CRAN +Date/Publication: 2014-11-25 08:52:58 diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000..6c9ec26 --- /dev/null +++ b/NAMESPACE @@ -0,0 +1,6 @@ +useDynLib(iccbeta) +#import(RcppArmadillo) +import(lme4) +importFrom("Rcpp", evalCpp) +exportPattern("^[[:alpha:]]+") + diff --git a/R/RcppExports.R b/R/RcppExports.R new file mode 100644 index 0000000..7108b70 --- /dev/null +++ b/R/RcppExports.R @@ -0,0 +1,18 @@ +# This file was generated by Rcpp::compileAttributes +# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 + +#' @title Compute ICCbeta intraclass correlation +#' @description Computes the proportion of variance attributed to group heterogeneity in slopes as described by Aguinis & Culpepper (2015). +#' The function uses lmer model information. +#' @usage icc_beta(X,l2id,T,vy) +#' @param X The design \code{matrix} of fixed effects from a lmer model. +#' @param l2id A \code{vector} that identifies group membership. The vector must be coded as a sequence from 1 to J, the number of groups. +#' @param T A \code{matrix} of the estimated variance-covariance matrix of a lmer model fit. +#' @return vy The variance of the dependent variable. +#' @author Steven A Culpepper +NULL + +icc_beta <- function(X, l2id, T, vy) { + .Call('iccbeta_icc_beta', PACKAGE = 'iccbeta', X, l2id, T, vy) +} + diff --git a/README.Rmd b/README.Rmd new file mode 100644 index 0000000..04c5623 --- /dev/null +++ b/README.Rmd @@ -0,0 +1,22 @@ +--- +output: github_document +--- + + + +```{r, echo = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>", + fig.path = "README-" +) +``` + +[![Travis-CI Build Status](https://travis-ci.org/tmsalab/iccbeta.svg?branch=master)](https://travis-ci.org/tmsalab/iccbeta)[![CRAN RStudio mirror downloads](http://cranlogs.r-pkg.org/badges/iccbeta)](http://www.r-pkg.org/pkg/iccbeta)[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/iccbeta)](https://cran.r-project.org/package=iccbeta) + +# `iccbeta` R Package + +A function and vignettes for computing an intraclass correlation described in +Aguinis & Culpepper (in press). iccbeta quantifies the share of variance in a +dependent variable that is attributed to group heterogeneity in slopes. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..c26e402 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ + + +[![Travis-CI Build Status](https://travis-ci.org/tmsalab/iccbeta.svg?branch=master)](https://travis-ci.org/tmsalab/iccbeta)[![CRAN RStudio mirror downloads](http://cranlogs.r-pkg.org/badges/iccbeta)](http://www.r-pkg.org/pkg/iccbeta)[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/iccbeta)](https://cran.r-project.org/package=iccbeta) + +`iccbeta` R Package +=================== + +A function and vignettes for computing an intraclass correlation described in Aguinis & Culpepper (in press). iccbeta quantifies the share of variance in a dependent variable that is attributed to group heterogeneity in slopes. diff --git a/data/Hofmann.rda b/data/Hofmann.rda new file mode 100644 index 0000000..904e3bb Binary files /dev/null and b/data/Hofmann.rda differ diff --git a/data/simICCdata.rda b/data/simICCdata.rda new file mode 100644 index 0000000..132846f Binary files /dev/null and b/data/simICCdata.rda differ diff --git a/iccbeta.Rproj b/iccbeta.Rproj new file mode 100644 index 0000000..7a06566 --- /dev/null +++ b/iccbeta.Rproj @@ -0,0 +1,17 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 4 +Encoding: UTF-8 + +RnwWeave: knitr +LaTeX: pdfLaTeX + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source diff --git a/inst/CITATION b/inst/CITATION new file mode 100644 index 0000000..28a5fcd --- /dev/null +++ b/inst/CITATION @@ -0,0 +1,12 @@ +citHeader("To cite package iccbeta in publications use:") + +citEntry(entry="Article", + title = "An expanded decision making procedure for examining cross-level interaction effects with multilevel modeling", + author = personList(as.person("Herman Aguinis"), + as.person("Steven Andrew Culpepper")), + journal = "Organization Research Methods", + year = "in press", + url = "http://mypage.iu.edu/~haguinis/pubs.html", + pdf = "http://mypage.iu.edu/~haguinis/ORMinpress.pdf", + textVersion = "Agunis, H. & Culpepper, S. A. (in press). An expanded decision making procedure for examining cross-level interaction effects with multilevel modeling. Organizational Research Methods.") + diff --git a/man/Hofmann.Rd b/man/Hofmann.Rd new file mode 100644 index 0000000..2055839 --- /dev/null +++ b/man/Hofmann.Rd @@ -0,0 +1,71 @@ +\name{Hofmann} +\alias{Hofmann} +\docType{data} +\title{ +A multilevel dataset from Hofmann, Griffin, and Gavin (2000). +} +\description{ +A multilevel dataset from Hofmann, Griffin, and Gavin (2000). +} +\usage{data(Hofmann)} +\format{ + A data frame with 1,000 observations and 7 variables. + \describe{ + \item{\code{id}}{a numeric vector of group ids.} + \item{\code{helping}}{a numeric vector of the helping outcome variable construct.} + \item{\code{mood}}{a level 1 mood predictor.} + \item{\code{mood_grp_mn}}{a level 2 variable of the group mean of mood.} + \item{\code{cohesion}}{a level 2 covariate measuring cohesion.} + \item{\code{mood_grp_cent}}{group-mean centered mood predictor.} + \item{\code{mood_grd_cent}}{grand-mean centered mood predictor.} + } +} +%\details{ +%% ~~ If necessary, more details than the __description__ above ~~ +%} +\author{ +Steven Andrew Culpepper, +Herman Aguinis + +Maintainer: Steven Andrew Culpepper +} +\source{ +Hofmann, D.A., Griffin, M.A., & Gavin, M.B. (2000). The application of hierarchical linear modeling to management research. In K.J. Klein, & S.W.J. Kozlowski (Eds.), Multilevel theory, research, and methods in organizations: Foundations, extensions, and new directions (pp. 467-511). Hoboken, NJ: Jossey-Bass. +} +\references{ +Aguinis, H., & Culpepper, S.A. (in press). An expanded decision making procedure for examining cross-level interaction effects with multilevel modeling. \emph{Organizational Research Methods}. Available at: \url{http://mypage.iu.edu/~haguinis/pubs.html} +} +\seealso{ +\code{\link[lme4]{lmer}}, \code{\link{model.matrix}}, \code{\link[lme4]{VarCorr}}, \code{\link[RLRsim]{LRTSim}}, \code{\link{simICCdata}} +} +\examples{ +\dontrun{ +data(Hofmann) + require(lme4) + + #Random-Intercepts Model + lmmHofmann0 = lmer(helping ~ (1|id),data=Hofmann) + vy_Hofmann = var(Hofmann[,'helping']) + #computing icca + VarCorr(lmmHofmann0)$id[1,1]/vy_Hofmann + + #Estimating Group-Mean Centered Random Slopes Model, no level 2 variables + lmmHofmann1 <- lmer(helping ~ mood_grp_cent + (mood_grp_cent |id),data=Hofmann,REML=F) + X_Hofmann = model.matrix(lmmHofmann1) + P = ncol(X_Hofmann) + T1_Hofmann = VarCorr(lmmHofmann1)$id[1:P,1:P] + #computing iccb + icc_beta(X_Hofmann,Hofmann[,'id'],T1_Hofmann,vy_Hofmann)$rho_beta + + #Performing LR test + #Need to install 'RLRsim' package + library('RLRsim') + lmmHofmann1a <- lmer(helping ~ mood_grp_cent + (1 |id),data=Hofmann,REML=F) + obs.LRT <- 2*(logLik(lmmHofmann1)-logLik(lmmHofmann1a))[1] + X <- getME(lmmHofmann1,"X") + Z <- t(as.matrix(getME(lmmHofmann1,"Zt"))) + sim.LRT <- LRTSim(X, Z, 0, diag(ncol(Z))) + (pval <- mean(sim.LRT > obs.LRT)) +} + } +\keyword{datasets} diff --git a/man/icc_beta.Rd b/man/icc_beta.Rd new file mode 100644 index 0000000..b4e46eb --- /dev/null +++ b/man/icc_beta.Rd @@ -0,0 +1,84 @@ +\name{icc_beta} +\alias{icc_beta} +\docType{package} +\title{ +Intraclass correlation used to assess variability of lower-order relationships across higher-order processes/units. +} +\description{ +A function and vignettes for computing the intraclass correlation described in Aguinis & Culpepper (in press). iccbeta quantifies the share of variance in an outcome variable that is attributed to heterogeneity in slopes due to higher-order processes/units. +} +\usage{ +icc_beta(X, l2id, T, vy) +} + +\arguments{ +\item{X}{The design \code{matrix} of fixed effects from a lmer model.} + +\item{l2id}{A \code{vector} that identifies group membership. The vector must be coded as a sequence of integers from 1 to J, the number of groups.} + +\item{T}{A \code{matrix} of the estimated variance-covariance matrix of a lmer model fit.} + +\item{vy}{The variance of the outcome variable.} +} + +\author{ +Steven Andrew Culpepper, +Herman Aguinis + +Maintainer: Steven Andrew Culpepper +} + +\references{ +Aguinis, H., & Culpepper, S.A. (in press). An expanded decision making procedure for examining cross-level interaction effects with multilevel modeling. \emph{Organizational Research Methods}. Available at: \url{http://mypage.iu.edu/~haguinis/pubs.html} +} +\seealso{ +\code{\link[lme4]{lmer}}, \code{\link{model.matrix}}, \code{\link[lme4]{VarCorr}}, \code{\link[RLRsim]{LRTSim}}, \code{\link{Hofmann}}, \code{\link{simICCdata}} +} +\examples{ +\dontrun{ +#Simulated Data Example from Aguinis & Culpepper (in press) +data(simICCdata) + require(lme4) + + #computing icca + vy = var(simICCdata$Y) + lmm0 <- lmer(Y ~ (1|l2id),data=simICCdata,REML=F) + VarCorr(lmm0)$l2id[1,1]/vy + + #Estimating random slopes model + lmm1 <- lmer(Y~I(X1-m_X1)+I(X2-m_X2) +(I(X1-m_X1)+I(X2-m_X2)|l2id),data=simICCdata2,REML=F) + X = model.matrix(lmm1) + p=ncol(X) + T1 = VarCorr(lmm1) $l2id[1:p,1:p] + #computing iccb + #Notice '+1' because icc_beta assumes l2ids are from 1 to 30. + icc_beta(X,simICCdata2$l2id+1,T1,vy)$rho_beta + +#Hofmann et al. (2000) Example +data(Hofmann) + require(lme4) + + #Random-Intercepts Model + lmmHofmann0 = lmer(helping ~ (1|id),data=Hofmann) + vy_Hofmann = var(Hofmann[,'helping']) + #computing icca + VarCorr(lmmHofmann0)$id[1,1]/vy_Hofmann + + #Estimating Group-Mean Centered Random Slopes Model, no level 2 variables + lmmHofmann1 <- lmer(helping ~ mood_grp_cent + (mood_grp_cent |id),data=Hofmann,REML=F) + X_Hofmann = model.matrix(lmmHofmann1) + P = ncol(X_Hofmann) + T1_Hofmann = VarCorr(lmmHofmann1)$id[1:P,1:P] + #computing iccb + icc_beta(X_Hofmann,Hofmann[,'id'],T1_Hofmann,vy_Hofmann)$rho_beta + + #Performing LR test + library('RLRsim') + lmmHofmann1a <- lmer(helping ~ mood_grp_cent + (1 |id),data=Hofmann,REML=F) + obs.LRT <- 2*(logLik(lmmHofmann1)-logLik(lmmHofmann1a))[1] + X <- getME(lmmHofmann1,"X") + Z <- t(as.matrix(getME(lmmHofmann1,"Zt"))) + sim.LRT <- LRTSim(X, Z, 0, diag(ncol(Z))) + (pval <- mean(sim.LRT > obs.LRT)) +} +} diff --git a/man/iccbeta-package.Rd b/man/iccbeta-package.Rd new file mode 100644 index 0000000..289f77f --- /dev/null +++ b/man/iccbeta-package.Rd @@ -0,0 +1,79 @@ +\name{iccbeta-package} +\alias{iccbeta-package} +\alias{iccbeta} +\docType{package} +\title{ +Intraclass correlation used to assess variability of lower-order relationships across higher-order processes/units. +} +\description{ +A function and vignettes for computing the intraclass correlation described in Aguinis & Culpepper (in press). iccbeta quantifies the share of variance in an outcome variable that is attributed to heterogeneity in slopes due to higher-order processes/units. +} +\details{ +\tabular{ll}{ +Package: \tab iccbeta\cr +Type: \tab Package\cr +Version: \tab 1.0\cr +Date: \tab 2014-11-21\cr +License: \tab GPL (>= 2)\cr +} +} +\author{ +Steven Andrew Culpepper, +Herman Aguinis + +Maintainer: Steven Andrew Culpepper +} +\references{ +Aguinis, H., & Culpepper, S.A. (in press). An expanded decision making procedure for examining cross-level interaction effects with multilevel modeling. \emph{Organizational Research Methods}. Available at: \url{http://mypage.iu.edu/~haguinis/pubs.html} +} + +\keyword{ package } + +\examples{ +\dontrun{ +#Simulated Data Example +data(simICCdata) + require(lme4) + + #computing icca + vy = var(simICCdata$Y) + lmm0 <- lmer(Y ~ (1|l2id),data=simICCdata,REML=F) + VarCorr(lmm0)$l2id[1,1]/vy + + #Estimating random slopes model + lmm1 <- lmer(Y~I(X1-m_X1)+I(X2-m_X2) +(I(X1-m_X1)+I(X2-m_X2)|l2id),data=simICCdata2,REML=F) + X = model.matrix(lmm1) + p=ncol(X) + T1 = VarCorr(lmm1) $l2id[1:p,1:p] + #computing iccb + #Notice '+1' because icc_beta assumes l2ids are from 1 to 30. + icc_beta(X,simICCdata2$l2id+1,T1,vy)$rho_beta + +#Hofmann 2000 Example +data(Hofmann) + require(lme4) + + #Random-Intercepts Model + lmmHofmann0 = lmer(helping ~ (1|id),data=Hofmann) + vy_Hofmann = var(Hofmann[,'helping']) + #computing icca + VarCorr(lmmHofmann0)$id[1,1]/vy_Hofmann + + #Estimating Group-Mean Centered Random Slopes Model, no level 2 variables + lmmHofmann1 <- lmer(helping ~ mood_grp_cent + (mood_grp_cent |id),data=Hofmann,REML=F) + X_Hofmann = model.matrix(lmmHofmann1) + P = ncol(X_Hofmann) + T1_Hofmann = VarCorr(lmmHofmann1)$id[1:P,1:P] + #computing iccb + icc_beta(X_Hofmann,Hofmann[,'id'],T1_Hofmann,vy_Hofmann)$rho_beta + + #Performing LR test + library('RLRsim') + lmmHofmann1a <- lmer(helping ~ mood_grp_cent + (1 |id),data=Hofmann,REML=F) + obs.LRT <- 2*(logLik(lmmHofmann1)-logLik(lmmHofmann1a))[1] + X <- getME(lmmHofmann1,"X") + Z <- t(as.matrix(getME(lmmHofmann1,"Zt"))) + sim.LRT <- LRTSim(X, Z, 0, diag(ncol(Z))) + (pval <- mean(sim.LRT > obs.LRT)) +} +} diff --git a/man/simICCdata.Rd b/man/simICCdata.Rd new file mode 100644 index 0000000..e8b36d6 --- /dev/null +++ b/man/simICCdata.Rd @@ -0,0 +1,60 @@ +\name{simICCdata} +\alias{simICCdata} +\docType{data} +\title{ +Simulated data example from Aguinis and Culpepper (in press). +} +\description{ +A simulated data example from Aguinis and Culpepper (in press) to demonstrate the icc_beta function for computing the proportion of variance in the outcome variable that is attributed to heterogeneity in slopes due to higher-order processes/units. +} +\usage{data(simICCdata)} +\format{ + A data frame with 900 observations (i.e., 30 observations nested within 30 groups) on the following 6 variables. + \describe{ + \item{\code{l1id}}{A within group ID variable.} + \item{\code{l2id}}{A group ID variable.} + \item{\code{one}}{A column of 1's for the intercept.} + \item{\code{X1}}{A simulated level 1 predictor.} + \item{\code{X2}}{A simulated level 1 predictor.} + \item{\code{Y}}{A simulated outcome variable.} + } +} +\details{ +See Aguinis and Culpepper (in press) for the model used to simulate the dataset. +} +\author{ +Steven Andrew Culpepper, +Herman Aguinis + +Maintainer: Steven Andrew Culpepper +} +\source{ +Aguinis, H., & Culpepper, S.A. (in press). An expanded decision making procedure for examining cross-level interaction effects with multilevel modeling. \emph{Organizational Research Methods}. Available at: \url{http://mypage.iu.edu/~haguinis/pubs.html} +} +%\references{ +%% ~~ possibly secondary sources and usages ~~ +%} +\seealso{ +\code{\link[lme4]{lmer}}, \code{\link{model.matrix}}, \code{\link[lme4]{VarCorr}}, \code{\link[RLRsim]{LRTSim}}, \code{\link{Hofmann}} +} +\examples{ +\dontrun{ +data(simICCdata) + require(lme4) + + #computing icca + vy = var(simICCdata$Y) + lmm0 <- lmer(Y ~ (1|l2id),data=simICCdata,REML=F) + VarCorr(lmm0)$l2id[1,1]/vy + + #Estimating random slopes model + lmm1 <- lmer(Y~I(X1-m_X1)+I(X2-m_X2) +(I(X1-m_X1)+I(X2-m_X2)|l2id),data=simICCdata2,REML=F) + X = model.matrix(lmm1) + p=ncol(X) + T1 = VarCorr(lmm1) $l2id[1:p,1:p] + #computing iccb + #Notice '+1' because icc_beta assumes l2ids are from 1 to 30. + icc_beta(X,simICCdata2$l2id+1,T1,vy)$rho_beta +} +} +\keyword{datasets} diff --git a/src/Makevars b/src/Makevars new file mode 100644 index 0000000..22ebc63 --- /dev/null +++ b/src/Makevars @@ -0,0 +1 @@ +PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) diff --git a/src/Makevars.win b/src/Makevars.win new file mode 100644 index 0000000..9542baf --- /dev/null +++ b/src/Makevars.win @@ -0,0 +1,2 @@ + +PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp new file mode 100644 index 0000000..8639d59 --- /dev/null +++ b/src/RcppExports.cpp @@ -0,0 +1,26 @@ +// This file was generated by Rcpp::compileAttributes +// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 + +#include +#include + +using namespace Rcpp; + +// icc_beta +Rcpp::List icc_beta(const arma::mat& X, const arma::vec& l2id, const arma::mat& T, double vy); +RcppExport SEXP iccbeta_icc_beta(SEXP XSEXP, SEXP l2idSEXP, SEXP TSEXP, SEXP vySEXP) { +BEGIN_RCPP + SEXP __sexp_result; + { + Rcpp::RNGScope __rngScope; + Rcpp::traits::input_parameter< const arma::mat& >::type X(XSEXP ); + Rcpp::traits::input_parameter< const arma::vec& >::type l2id(l2idSEXP ); + Rcpp::traits::input_parameter< const arma::mat& >::type T(TSEXP ); + Rcpp::traits::input_parameter< double >::type vy(vySEXP ); + Rcpp::List __result = icc_beta(X, l2id, T, vy); + PROTECT(__sexp_result = Rcpp::wrap(__result)); + } + UNPROTECT(1); + return __sexp_result; +END_RCPP +} diff --git a/src/icc_beta.cpp b/src/icc_beta.cpp new file mode 100644 index 0000000..f3d55e5 --- /dev/null +++ b/src/icc_beta.cpp @@ -0,0 +1,51 @@ +#include +#include +// [[Rcpp::depends(RcppArmadillo)]] +using namespace Rcpp; + +//' @title Compute ICCbeta intraclass correlation +//' @description Computes the proportion of variance attributed to group heterogeneity in slopes as described by Aguinis & Culpepper (2015). +//' The function uses lmer model information. +//' @usage icc_beta(X,l2id,T,vy) +//' @param X The design \code{matrix} of fixed effects from a lmer model. +//' @param l2id A \code{vector} that identifies group membership. The vector must be coded as a sequence from 1 to J, the number of groups. +//' @param T A \code{matrix} of the estimated variance-covariance matrix of a lmer model fit. +//' @return vy The variance of the dependent variable. +//' @author Steven A Culpepper + +// [[Rcpp::export]] +Rcpp::List icc_beta(const arma::mat& X,const arma::vec& l2id,const arma::mat& T,double vy){ + unsigned int N = l2id.n_elem; + unsigned int p = X.n_cols; + unsigned int J=max(l2id); + arma::mat SUMS = arma::zeros(J,p); + arma::mat means = arma::zeros(J,p); + arma::vec Nj = arma::zeros(J); + arma::vec ONE = arma::ones(N); + + for(unsigned int i=0;i0.0){ + means.row(j) = SUMS.row(j)/Nj(j); + } + } + + arma::mat XcpXc = arma::zeros(p,p); + for(unsigned int i=0;i