├── DESCRIPTION ├── LICENSE ├── NAMESPACE ├── R ├── misc.R ├── sim_data.R └── stim_lists.R ├── README.Rmd ├── docs ├── LICENSE-text.html ├── authors.html ├── docsearch.css ├── docsearch.js ├── index.html ├── link.svg ├── pkgdown.css ├── pkgdown.js ├── pkgdown.yml └── reference │ ├── compose_data.html │ ├── contr.dev.html │ ├── design_formula.html │ ├── fac_counts.html │ ├── gen_pop.html │ ├── index.html │ ├── sim_norm.html │ ├── stim_lists.html │ ├── term_names.html │ ├── trial_lists.html │ └── with_dev_pred.html └── man ├── compose_data.Rd ├── contr.dev.Rd ├── design_formula.Rd ├── fac_counts.Rd ├── gen_pop.Rd ├── sim_norm.Rd ├── stim_lists.Rd ├── term_names.Rd ├── trial_lists.Rd └── with_dev_pred.Rd /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: funfact 2 | Title: Functions for factorial experiments 3 | Description: Provides functionality for planning and analyzing 4 | factorially-designed experiments involving sampling of subjects 5 | and stimulus materials (items). 6 | Version: 0.300 7 | License: GPL-2 8 | Author: Dale J. Barr 9 | Maintainer: Dale J. Barr 10 | Imports: clusterGeneration, MASS 11 | RoxygenNote: 6.1.1 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(compose_data) 4 | export(contr.dev) 5 | export(design_formula) 6 | export(fac_counts) 7 | export(gen_pop) 8 | export(sim_norm) 9 | export(stim_lists) 10 | export(term_names) 11 | export(trial_lists) 12 | export(with_dev_pred) 13 | importFrom(MASS,mvrnorm) 14 | importFrom(clusterGeneration,genPositiveDefMat) 15 | -------------------------------------------------------------------------------- /R/misc.R: -------------------------------------------------------------------------------- 1 | #' Deviation-Coded Contrast Matrices 2 | #' 3 | #' Return a matrix of deviation-coded contrasts. 4 | #' 5 | #' @param n a vector of levels for a factor, or the number of levels. 6 | #' @param base an integer specifying which group is considered the 7 | #' baseline group. Ignored if 'contrasts' is \code{FALSE}. 8 | #' @param contrasts a logical indicating whether contrasts should be computed. 9 | #' 10 | #' @export 11 | contr.dev <- function(n, base = 1, contrasts = TRUE) { 12 | if (length(n) <= 1L) { 13 | if (is.numeric(n) && (length(n) == 1L) && (n > 1L)) 14 | levels <- seq_len(n) 15 | else stop("not enough degrees of freedom to define contrasts") 16 | } else { 17 | levels <- n 18 | } 19 | ctreat <- contr.treatment(levels, base, contrasts) 20 | mx <- apply(ctreat, 2, scale, scale = FALSE) 21 | dimnames(mx) <- dimnames(ctreat) 22 | mx 23 | } 24 | 25 | #' Calculate marginal and cell counts for factorially-designed data 26 | #' 27 | #' @param iv_names Names of independent variables in data.frame given by \code{dat}. 28 | #' @param dat A data frame 29 | #' @param unit_names Names of the fields containing sampling units (subjects, items) 30 | #' @return a list, the elements of which have the marginal/cell counts for each factor in the design 31 | #' @export 32 | fac_counts <- function(iv_names, dat, unit_names = c("subj_id", "item_id")) { 33 | fac_info <- attr(terms(as.formula(paste0("~", paste(iv_names, collapse = "*")))), "factors") 34 | rfx <- sapply(unit_names, function(this_unit) { 35 | ## figure out how many things are replicated by unit, how many times 36 | rep_mx <- xtabs(paste0("~", paste(iv_names, collapse = "+"), "+", this_unit), dat) 37 | lvec <- lapply(colnames(fac_info), function(cx) { 38 | x <- fac_info[, cx, drop = FALSE] 39 | ix <- seq_along(x)[as.logical(x)] 40 | ## create margin table 41 | marg_mx <- apply(rep_mx, c(ix, length(dim(rep_mx))), sum) 42 | mmx <- apply(marg_mx, length(dim(marg_mx)), c) 43 | }) 44 | names(lvec) <- colnames(fac_info) 45 | return(lvec) 46 | ## lvec <- apply(fac_info, 2, function(x) { 47 | ## ix <- seq_along(x)[as.logical(x)] 48 | ## ## create margin table 49 | ## marg_mx <- apply(rep_mx, c(ix, length(dim(rep_mx))), sum) 50 | ## mmx <- apply(marg_mx, length(dim(marg_mx)), c) 51 | ## }) 52 | }, simplify = FALSE) 53 | return(rfx) 54 | } 55 | 56 | #' Generate numerical deviation-coded predictors 57 | #' 58 | #' Add deviation-coded predictors to data frame. 59 | #' 60 | #' @param dat A data frame with columns containing factors to be converted. 61 | #' @param iv_names Names of the variables to be converted. 62 | #' 63 | #' @return A data frame including additional deviation coded predictors. 64 | #' 65 | #' @examples 66 | #' with_dev_pred(stim_lists(list(ivs = c(A = 3))), "A") 67 | #' @export 68 | with_dev_pred <- function(dat, iv_names = NULL) { 69 | if (is.null(iv_names)) { 70 | iv_names <- names(dat) 71 | } else {} 72 | mform <- as.formula(paste0("~", paste(iv_names, collapse = "+"))) 73 | 74 | cont <- lapply(iv_names, function(.x) { 75 | if (is.factor(dat[[.x]])) { 76 | contr.dev(levels(dat[[.x]])) 77 | } else { 78 | contr.dev(sort(unique(dat[[.x]]))) 79 | } 80 | }) 81 | names(cont) <- iv_names 82 | 83 | mmx <- model.matrix(mform, dat, contrasts.arg = cont)[, -1, drop = FALSE] 84 | cbind(dat, mmx) 85 | } 86 | 87 | check_design_args <- function(design_args) { 88 | ## TODO check integrity of design args 89 | required_elements <- c("ivs") 90 | missing_elements <- setdiff(required_elements, names(design_args)) 91 | if (length(missing_elements) > 0) { 92 | stop("'design_args' missing element(s): ", 93 | paste(missing_elements, collapse = ", ")) 94 | } else {} 95 | return(TRUE) 96 | } 97 | 98 | #' Get names for predictors in factorial design 99 | #' 100 | #' Get the names for the numerical predictors corresponding to all 101 | #' main effects and interactions of categorical IVs in a factorial 102 | #' design. 103 | #' 104 | #' @param design_args A list with experimental design information (see \code{link{stim_lists}}) 105 | #' @param design_formula A formula (default NULL, constructs from \code{design_args}) 106 | #' @param contr_type Name of formula for generating contrasts (default "contr.dev") 107 | #' @return A character vector with names of all the terms 108 | #' @export 109 | term_names <- function(design_args, 110 | design_formula = NULL, 111 | contr_type = "contr.dev") { 112 | check_design_args(design_args) 113 | plists <- stim_lists(design_args) 114 | 115 | cont <- lapply(design_args[["ivs"]], function(.x) { 116 | do.call(contr_type, list(.x)) 117 | }) 118 | 119 | names(cont) <- names(design_args[["ivs"]]) 120 | if (is.null(design_formula)) 121 | design_formula <- as.formula(paste0("~", 122 | paste(names(design_args[["ivs"]]), 123 | collapse = " * "))) 124 | suppressWarnings(mmx <- model.matrix(design_formula, plists, 125 | contrasts.arg = cont)) 126 | return(colnames(mmx)) 127 | } 128 | 129 | #' Get the GLM formula for a factorially-designed experiment 130 | #' 131 | #' Get the formula corresponding to the general linear model for a 132 | #' factorially designed experiment, with maximal random effects. 133 | #' 134 | #' @param design_args A list with experimental design information (see \code{link{stim_lists}}) 135 | #' @param n_subj Number of subjects 136 | #' @param dv_name Name of dependent variable; \code{NULL} (default) for one-sided formula 137 | #' @param lme4_format Do you want the results combined as the model formula for a \code{lme4} model? (default \code{TRUE}) 138 | #' @return A formula, character string, or list, depending 139 | #' @export 140 | design_formula <- function(design_args, 141 | n_subj = NULL, 142 | dv_name = NULL, 143 | lme4_format = TRUE) { 144 | iv_names <- names(design_args[["ivs"]]) 145 | 146 | fixed <- paste(iv_names, collapse = " * ") 147 | 148 | fac_cnts <- fac_counts(iv_names, trial_lists(design_args, subjects = n_subj)) 149 | fac_info <- attr(terms(as.formula(paste0("~", paste(iv_names, collapse = "*")))), "factors") 150 | 151 | ## figure out whether data are multilevel 152 | is_unit_ml <- sapply(fac_cnts, function(lx) { 153 | is_multilevel <- FALSE 154 | ## first condition: more than one subject? (item?) 155 | ranfac_has_multiple_levels <- any(sapply(lx, ncol) > 1) 156 | if (ranfac_has_multiple_levels) { 157 | is_fac_multilevel <- sapply(lx, function(fx) { 158 | any(apply(fx, 2, function(.x) any(.x > 1L))) 159 | }) 160 | is_multilevel <- any(is_fac_multilevel) 161 | } 162 | is_multilevel 163 | }) 164 | 165 | if (!any(is_unit_ml)) { 166 | ## data are not multilevel 167 | rfx <- NULL 168 | } else { 169 | ## data are multilevel 170 | rfx <- lapply(fac_cnts[is_unit_ml], function(lx) { 171 | lvec <- sapply(lx, function(mx) { 172 | as.logical(prod(apply(mx, 2, function(xx) all(xx > 1)))) 173 | }) 174 | res <- fac_info[, lvec, drop = FALSE] 175 | keep_term <- rep(TRUE, ncol(res)) 176 | ## try to simplify the formula 177 | for (cx in rev(seq_len(ncol(res))[-1])) { 178 | drop_term <- sapply(seq_len(cx - 1), function(ccx) { 179 | identical(as.logical(res[, ccx, drop = FALSE]) | as.logical(res[, cx, drop = FALSE]), 180 | as.logical(res[, cx, drop = FALSE])) 181 | }) 182 | keep_term[seq_len(cx - 1)] <- keep_term[seq_len(cx - 1)] & (!drop_term) 183 | } 184 | fterms <- apply(res[, keep_term, drop = FALSE], 2, function(llx) { 185 | paste(names(llx)[as.logical(llx)], collapse = " * ") 186 | }) 187 | need_int <- any(apply(lx[[1]], 2, sum) > 1) 188 | fterms2 <- if (need_int) c("1", fterms) else fterms 189 | paste(fterms2, collapse = " + ") 190 | }) 191 | } 192 | 193 | form_list <- c(list(fixed = fixed), rfx) 194 | 195 | if (lme4_format) { 196 | form_str <- paste0(dv_name, " ~ ", form_list[["fixed"]]) 197 | if (length(form_list) > 1L) { 198 | form_str <- paste0(form_str, " + ", 199 | paste(sapply(names(form_list[-1]), 200 | function(nx) paste0("(", rfx[[nx]], " | ", nx, ")")), 201 | collapse = " + ")) 202 | } 203 | result <- as.formula(form_str) 204 | } else { 205 | result <- lapply(form_list, function(x) formula(paste0("~", x))) 206 | } 207 | 208 | return(result) 209 | } 210 | -------------------------------------------------------------------------------- /R/sim_data.R: -------------------------------------------------------------------------------- 1 | #' Generate population for data simulation 2 | #' 3 | #' @param design_args list specifying experimental design (see 4 | #' \code{\link{stim_lists}}); for best results, n_item should be 5 | #' 2*minimum number of items needed for the design 6 | #' @param n_subj number of subjects (for defining random effects 7 | #' structure; for best results, should be 2*number of stimulus lists 8 | #' @param fixed_ranges list of 2-element vectors (min, max) defining 9 | #' ranges of fixed effect parameters 10 | #' @param var_range 2-element vector defining range (min, max) of 11 | #' random effect variances 12 | #' @param err_range 2-element vector defining range of error variance 13 | #' @return list with parameters for data generation 14 | #' @seealso \code{\link{sim_norm}} 15 | #' @examples 16 | #' 17 | #' dargs <- list(ivs = c(A = 2, B = 2), n_item = 8) 18 | #' gen_pop(dargs, 8) 19 | #' @importFrom clusterGeneration genPositiveDefMat 20 | #' @export 21 | gen_pop <- function(design_args, 22 | n_subj = NULL, 23 | fixed_ranges = NULL, 24 | var_range = c(0, 3), 25 | err_range = c(0, 6)) { 26 | tdat <- trial_lists(design_args, n_subj) 27 | if ("n_rep" %in% colnames(tdat)) { 28 | design_args[["ivs"]] <- c(as.list(design_args[["ivs"]]), list(n_rep = unique(tdat[["n_rep"]]))) 29 | } else {} 30 | forms <- design_formula(design_args, n_subj, lme4_format = FALSE) 31 | tnames <- lapply(forms, function(x) term_names(design_args, x)) 32 | err_var <- runif(1, err_range[1], err_range[2]) 33 | ## generate random effects 34 | rfx <- lapply(tnames[-1], function(x) { 35 | mx <- clusterGeneration::genPositiveDefMat(length(x), 36 | covMethod = "onion", 37 | rangeVar = var_range)$Sigma 38 | dimnames(mx) <- list(x, x) 39 | return(mx) 40 | }) 41 | ## generate fixed effects 42 | if (!is.null(fixed_ranges)) { 43 | if (length(fixed_ranges) != length(tnames[["fixed"]])) { 44 | stop("fixed_ranges must have ", length(tnames[["fixed"]]), 45 | " elements, corresponding to variables ", 46 | paste(tnames[["fixed"]], collapse = ", ")) 47 | } else {} 48 | } else { 49 | fixed_ranges <- lapply(seq_along(tnames[["fixed"]]), function(x) {c(0, 3)}) 50 | names(fixed_ranges) <- tnames[["fixed"]] 51 | } 52 | if (!is.null(names(fixed_ranges)) && 53 | !identical(names(fixed_ranges), tnames[["fixed"]])) { 54 | warning("names of 'fixed_ranges' elements do not match variable names: ", 55 | paste(tnames[["fixed"]], collapse = ", ")) 56 | } else {} 57 | return(list(fixed = sapply(fixed_ranges, function(x) runif(1, x[1], x[2])), 58 | subj_rfx = rfx[["subj_id"]], 59 | item_rfx = rfx[["item_id"]], 60 | err_var = err_var)) 61 | } 62 | 63 | #' Sample data from population with normal error variance 64 | #' 65 | #' @param design_args experiment design (see 66 | #' \code{\link{stim_lists}}); must contain sub-element \code{n_item} 67 | #' defining the number of items. 68 | #' @param n_subj number of subjects 69 | #' @param params list with parameters defining population 70 | #' (\code{fixed}, \code{subj_rfx}, \code{item_rfx}), and 71 | #' \code{err_var}; normally generated by a call to 72 | #' \code{\link{gen_pop}}. 73 | #' @param contr_type name of function defining IV contrast type; see 74 | #' \code{?contrasts} 75 | #' @param verbose whether to return explicit information about 76 | #' individual random effects and residual error 77 | #' 78 | #' @return A data frame with simulated data 79 | #' @seealso \code{\link{gen_pop}} 80 | #' @examples 81 | #' design_args <- list(ivs = c(A = 2, B = 3), n_item = 18) 82 | #' pop_params <- gen_pop(design_args, 12) 83 | #' 84 | #' dat <- sim_norm(design_args, 12, pop_params) 85 | #' @importFrom MASS mvrnorm 86 | #' @export 87 | sim_norm <- function(design_args, 88 | n_subj, 89 | params, 90 | contr_type = "contr.dev", 91 | verbose = FALSE) { 92 | required_elements <- c("fixed", "subj_rfx", "item_rfx", "err_var") 93 | missing_elements <- setdiff(required_elements, names(params)) 94 | if (length(missing_elements) > 0) { 95 | stop("mcr.data was missing element(s): ", 96 | paste(missing_elements, collapse = ", ")) 97 | } else {} 98 | if (is.null(design_args[["n_item"]])) { 99 | stop("'n_item' not specified in 'design_args'") 100 | } else {} 101 | rfx <- mapply(function(x, n) { 102 | if (!is.null(x)) { 103 | MASS::mvrnorm(n, mu = rep(0, ncol(x)), x) 104 | } else { 105 | n 106 | } 107 | }, params[c("subj_rfx", "item_rfx")], 108 | c(n_subj, design_args[["n_item"]]), SIMPLIFY = FALSE) 109 | dat <- compose_data(design_args, 110 | fixed = params[["fixed"]], 111 | subj_rmx = rfx[["subj_rfx"]], 112 | item_rmx = rfx[["item_rfx"]], 113 | contr_type = contr_type, verbose = verbose) 114 | dat[["err"]] <- rnorm(nrow(dat), sd = sqrt(params[["err_var"]])) 115 | dat[["Y"]] <- dat[["Y"]] + dat[["err"]] 116 | return(dat) 117 | } 118 | -------------------------------------------------------------------------------- /R/stim_lists.R: -------------------------------------------------------------------------------- 1 | #' Generate stimulus presentation lists 2 | #' 3 | #' Generates counterbalanced presentation lists for factorially 4 | #' designed experiments involving stimulus presentation 5 | #' 6 | #' @param design_args A list describing the experimental design, which 7 | #' must have an element \code{ivs}, giving a named list of independent 8 | #' variables, with each list element a vector giving the levels of 9 | #' that IV, or a single number stating the number of desired levels. 10 | #' If any IVs are administered between-subjects or between-items, 11 | #' these should be named in list elements \code{between_subj} and 12 | #' \code{between_item} respectively. The argument \code{design_args} 13 | #' also can optionally include the following two elements: 14 | #' \code{n_item}, the desired number of stimulus items, which if 15 | #' unspecified, will result in lists with the minimum possible number 16 | #' of items; and \code{n_rep}, the number of repetitions of each 17 | #' stimulus item for each participant (default 1). 18 | #' @param as_one boolean (default \code{TRUE}) specifying whether the 19 | #' presentation lists are to be returned as a single data frame or as 20 | #' elements in a list object 21 | #' 22 | #' @return a single \code{data.frame} (default) with each list 23 | #' identified by \code{list_id} or a \code{list} of dataframes, 24 | #' depending on the value of \code{as_one} 25 | #' 26 | #' @examples 27 | #' stim_lists(list(ivs = c(A = 2, B = 2))) # 2x2 within-subjects within-item 28 | #' 29 | #' stim_lists(list(ivs = c(A = 2, B = 2, n_item = 16))) # same but w/more items 30 | #' 31 | #' stim_lists(list(ivs = c(A = 2, B = 2, n_item = 16, n_rep = 3))) 32 | #' 33 | #' # mixed by subject, fully within by item 34 | #' stim_lists(list(ivs = list(group = c("adult", "child"), 35 | #' task = c("easy", "hard")), 36 | #' between_subj = "group", 37 | #' n_item = 12)) 38 | #' 39 | #' # mixed by subject, mixed by item 40 | #' stim_lists(list(ivs = c(A = 2, B = 2), 41 | #' between_subj = "A", 42 | #' between_item = "B")) 43 | #' @export 44 | stim_lists <- function(design_args, 45 | as_one = TRUE) { 46 | fac_combine_levels <- function(vars, iv_list, dframe = TRUE) { 47 | row_indices <- rev(do.call("expand.grid", 48 | lapply(rev(vars), 49 | function(x) seq_along(iv_list[[x]])))) 50 | res <- mapply(function(x, y) x[y], iv_list[vars], row_indices) 51 | if (dframe) { 52 | as.data.frame(res, stringsAsFactors = FALSE) 53 | } else { 54 | res 55 | } 56 | } 57 | 58 | rotate_cells <- function(x, combine = FALSE) { 59 | res <- lapply(seq_len(nrow(x)), 60 | function(ix) x[c(ix:nrow(x), seq_len(ix - 1)), , drop = FALSE]) 61 | if (combine) { 62 | do.call("rbind", res) 63 | } else { 64 | res 65 | } 66 | } 67 | 68 | bs_combine <- function(dat, plists) { 69 | ## factorially combine across lists for between subject variables 70 | if (nrow(dat) == 0) { 71 | return(plists) 72 | } else {} 73 | res <- c(lapply(seq_len(nrow(dat)), function(rx) { 74 | if (length(plists) > 0) { 75 | lapply(plists, function(lx) { 76 | cbind(dat[rep(rx, nrow(lx)), , drop = FALSE], lx) 77 | }) 78 | } else { 79 | dat[rx, , drop = FALSE] 80 | } 81 | })) 82 | if (length(plists) > 0) { 83 | do.call("c", res) 84 | } else { 85 | res 86 | } 87 | } 88 | 89 | check_design_args(design_args) 90 | iv_names <- names(design_args[["ivs"]]) 91 | ## check whether elements of 'ivs' are numbers and convert to char vector 92 | ivs2 <- lapply(iv_names, function(nx) { 93 | x <- design_args[["ivs"]][[nx]] 94 | if ((length(x) == 1) && is.numeric(x)) { 95 | paste0(nx, seq_len(x)) 96 | } else {x} 97 | }) 98 | names(ivs2) <- iv_names 99 | 100 | item_within <- setdiff(names(ivs2), design_args[["between_item"]]) 101 | subj_within <- setdiff(names(ivs2), design_args[["between_subj"]]) 102 | ## iv_levels <- sapply(ivs2, length) # IS THIS NEEDED? 103 | 104 | ww_fac <- intersect(item_within, subj_within) 105 | 106 | ww_chunks <- rotate_cells(fac_combine_levels(intersect(item_within, subj_within), 107 | ivs2)) 108 | 109 | wb_chunks <- fac_combine_levels(intersect(subj_within, design_args[["between_item"]]), ivs2) 110 | 111 | ## combine the WSWI and WSBI chunks to create the base presentation lists 112 | if (nrow(wb_chunks) > 0) { 113 | if (length(ww_chunks) > 0) { 114 | base_plists <- lapply(ww_chunks, function(ww) { 115 | cbind(wb_chunks[rep(seq_len(nrow(wb_chunks)), each = nrow(ww)), , drop = FALSE], ww) 116 | }) 117 | } else { 118 | base_plists <- list(wb_chunks) 119 | } 120 | } else { 121 | base_plists <- ww_chunks 122 | } 123 | 124 | ## handle BSWI 125 | bswi <- fac_combine_levels(intersect(design_args[["between_subj"]], 126 | item_within), ivs2) 127 | bswi_lists <- bs_combine(bswi, base_plists) 128 | 129 | ## handle bsbi factors (if they exist) 130 | bsbi <- fac_combine_levels(intersect(design_args[["between_subj"]], 131 | design_args[["between_item"]]), ivs2) 132 | bsbi_lists <- bs_combine(bsbi, bswi_lists) 133 | div_fac <- if (nrow(bsbi)) nrow(bsbi) else 1 134 | n_item <- design_args[["n_item"]] 135 | if (is.null(design_args[["n_item"]])) { # dynamically choose minimum n_item 136 | if (length(bswi_lists) > 0) { 137 | n_item <- nrow(bswi_lists[[1]]) * div_fac 138 | } else { 139 | n_item <- div_fac 140 | } 141 | } else {} 142 | if (length(bswi_lists) > 0) { 143 | item_fac <- div_fac * nrow(bswi_lists[[1]]) 144 | if ((n_item %% item_fac) != 0) { 145 | stop("n_item must be a factor of ", item_fac) 146 | } else {} 147 | } else {} 148 | 149 | if (length(bsbi_lists) == 0) { 150 | bsbi_lists <- bswi_lists 151 | } else {} 152 | 153 | if ((n_item %% div_fac) != 0) stop("n_item must be a multiple of ", div_fac) 154 | 155 | rep_times <- if (length(bswi_lists) > 0) length(bswi_lists) else 1 156 | it_chunks <- rep(seq_len(div_fac), each = rep_times) 157 | it_lists <- split(seq_len(n_item), 158 | rep(seq_len(div_fac), each = n_item / div_fac))[it_chunks] 159 | 160 | plists <- mapply(function(x, y) { 161 | ix <- rep(seq_len(nrow(y)), each = length(x) / nrow(y)) 162 | cbind(item_id = x, y[ix, , drop = FALSE]) 163 | }, 164 | it_lists, bsbi_lists, SIMPLIFY = FALSE) 165 | 166 | n_rep <- design_args[["n_rep"]] 167 | if (is.null(design_args[["n_rep"]])) n_rep <- 1 168 | 169 | if (n_rep > 1) { 170 | plists <- lapply(plists, function(x) { 171 | data.frame(n_rep = as.character(paste0("r", rep(seq_len(n_rep), each = nrow(x)))), 172 | x[rep(seq_len(nrow(x)), n_rep), , drop = FALSE], 173 | check.names = FALSE, stringsAsFactors = FALSE) 174 | }) 175 | } else {} 176 | 177 | if (as_one) { 178 | res <- mapply(function(x, y) { 179 | cbind(list_id = x, y) 180 | }, 181 | seq_along(plists), plists, SIMPLIFY = FALSE) 182 | final_lists <- do.call("rbind", res) 183 | } else { 184 | final_lists <- plists 185 | } 186 | rownames(final_lists) <- NULL 187 | return(final_lists) 188 | } 189 | 190 | #' Generate trial lists from a stimulus presentation lists 191 | #' 192 | #' Merge stimulus presentation lists with subject data to create a 193 | #' trial list. 194 | #' 195 | #' @param design_args Stimulus presentation lists (see \code{\link{stim_lists}}). 196 | #' @param subjects One of the following three: (1) an integer 197 | #' specifying the desired number of subjects (must be a multiple of 198 | #' number of stimulus lists); (2) a data frame with assignment 199 | #' information (must include a column \code{subj_id}); or (3) 200 | #' \code{NULL}, in which case there will be one subject per list. 201 | #' @param seq_assign If TRUE, assignment of subjects to lists will 202 | #' be sequential rather than random (default is FALSE) 203 | #' 204 | #' @return A data frame containing all trial information. 205 | #' @export 206 | trial_lists <- function(design_args, 207 | subjects = NULL, seq_assign = FALSE) { 208 | sp_lists <- stim_lists(design_args) 209 | sp2 <- split(sp_lists, sp_lists[["list_id"]]) 210 | if (is.null(subjects)) { 211 | subjects <- length(sp2) 212 | } else {} 213 | if (is.numeric(subjects)) { 214 | if ((subjects %% length(sp2)) != 0) { 215 | stop("'subjects' must be a multiple of number of lists (", 216 | length(sp2), ")") 217 | } else {} 218 | list_ord = rep(seq_along(sp2), subjects / length(sp2)) 219 | if (!seq_assign) { 220 | list_ord = sample(list_ord) # randomize assignment to lists 221 | } else {} 222 | subj_dat <- data.frame(subj_id = seq_len(subjects), 223 | list_id = list_ord) 224 | } else { 225 | if (!is.data.frame(subjects)) { 226 | stop("'subjects' must be an integer, data.frame, or NULL") 227 | } else {} 228 | subj_dat <- subjects 229 | if (any(!("subj_id" %in% names(subj_dat)), 230 | !("list_id" %in% names(subj_dat)))) { 231 | stop("'subjects' must contain fields 'subj_id', 'list_id'") 232 | } else {} 233 | } 234 | res <- lapply(seq_len(nrow(subj_dat)), function(rx) { 235 | cbind(subj_id = subj_dat[rx, "subj_id"], 236 | sp2[[subj_dat[rx, "list_id"]]]) 237 | }) 238 | res2 <- do.call("rbind", res) 239 | rownames(res2) <- NULL 240 | return(res2) 241 | } 242 | 243 | #' Compose response data from fixed and random effects 244 | #' 245 | #' @param design_args List containing information about the experiment 246 | #' design; see \code{\link{stim_lists}} 247 | #' @param fixed vector of fixed effects 248 | #' @param subj_rmx matrix of by-subject random effects 249 | #' @param item_rmx matrix of by-item random effects 250 | #' @param verbose give debugging info (default = \code{FALSE}) 251 | #' @param contr_type contrast type (default "contr.dev"); see ?contrasts 252 | #' 253 | #' @details This will add together all of the fixed and random effects 254 | #' according to the linear model specified by the design. Note, 255 | #' however, that it does not add in any residual noise; for that, use 256 | #' the function \code{\link{sim_norm}}. 257 | #' 258 | #' @return a data frame containing response variable \code{Y}, the 259 | #' linear sum of all fixed and random effects. 260 | #' @export 261 | compose_data <- function(design_args, 262 | fixed = NULL, 263 | subj_rmx = NULL, 264 | item_rmx = NULL, 265 | verbose = FALSE, 266 | contr_type = "contr.dev") { 267 | 268 | ## utility function for doing matrix multiplication 269 | multiply_mx <- function(des_mx, rfx, row_ix, design_args) { 270 | ## make sure all cols in rfx are represented in des_mx 271 | diff_cols <- setdiff(colnames(rfx), colnames(des_mx)) 272 | if (length(diff_cols) != 0) { 273 | stop("column(s) '", paste(diff_cols, collapse = ", "), 274 | "' not represented in terms '", 275 | paste(term_names(design_args[["ivs"]], 276 | design_args[["between_subj"]], 277 | design_args[["between_item"]]), 278 | collapse = ", "), "'") 279 | } else {} 280 | 281 | reduced_des <- des_mx[, colnames(rfx), drop = FALSE] 282 | 283 | t_rfx <- t(rfx) 284 | res_vec <- vector("numeric", length(row_ix)) 285 | for (ix in unique(row_ix)) { 286 | lvec <- row_ix == ix 287 | res_vec[lvec] <- c(reduced_des[lvec, , drop = FALSE] %*% 288 | t_rfx[, ix, drop = FALSE]) 289 | } 290 | res_vec 291 | } 292 | 293 | ivs_nrep <- design_args[["ivs"]] 294 | if (!is.null(design_args[["n_rep"]])) { 295 | if (design_args[["n_rep"]] > 1) { 296 | ivs_nrep <- c(as.list(design_args[["ivs"]]), 297 | list(n_rep = paste0("r", seq_len(design_args[["n_rep"]])))) 298 | } else {} 299 | } else {} 300 | iv_names <- names(ivs_nrep) 301 | if (is.matrix(subj_rmx)) { 302 | tlists <- trial_lists(design_args, subjects = nrow(subj_rmx)) 303 | } else { 304 | tlists <- trial_lists(design_args, subjects = subj_rmx) 305 | } 306 | 307 | ## 308 | cont <- lapply(design_args[["ivs"]], function(.x) { 309 | do.call(contr_type, list(.x)) 310 | }) 311 | ## cont <- as.list(rep(contr_type, length(ivs_nrep))) 312 | names(cont) <- iv_names 313 | 314 | mmx <- model.matrix(as.formula(paste0("~", paste(iv_names, collapse = "*"))), 315 | tlists, 316 | contrasts.arg = cont) 317 | 318 | if (is.null(fixed)) { 319 | fixed <- runif(ncol(mmx), -3, 3) 320 | names(fixed) <- colnames(mmx) 321 | } else {} 322 | 323 | ## fixed component of Y 324 | fix_y <- c(mmx %*% fixed) # fixed component of Y 325 | 326 | if (is.matrix(subj_rmx)) { 327 | ## stop("Autogeneration of subj_rmx not implemented yet; please define 'subj_rmx'") 328 | if (nrow(subj_rmx) != length(unique(tlists[["subj_id"]]))) { 329 | stop("Argument 'subj_rmx' has ", nrow(subj_rmx), " rows; needs ", 330 | length(unique(tlists[["subj_id"]]))) 331 | } else {} 332 | sre <- multiply_mx(mmx, subj_rmx, tlists[["subj_id"]], design_args) 333 | } else { 334 | sre <- NULL 335 | } 336 | 337 | if (is.matrix(item_rmx)) { 338 | ## stop("Autogeneration of item_rmx not implemented yet; please define 'item_rmx'") 339 | if (nrow(item_rmx) != length(unique(tlists[["item_id"]]))) { 340 | stop("Argument 'item_rmx' has ", nrow(item_rmx), " rows; needs ", 341 | length(unique(tlists[["item_id"]]))) 342 | } else {} 343 | ire <- multiply_mx(mmx, item_rmx, tlists[["item_id"]], design_args) 344 | } else { 345 | ire <- NULL 346 | } 347 | 348 | comb_mx <- matrix(nrow = nrow(tlists), ncol = 0) 349 | if (verbose) { 350 | ## comb_mx <- cbind(fix_y = fix_y, sre = sre, ire = ire, err = err) 351 | comb_mx <- cbind(fix_y = fix_y, sre = sre, ire = ire) 352 | } else {} 353 | ## cbind(tlists, Y = fix_y + sre + ire + err, comb_mx) 354 | 355 | yvals <- fix_y 356 | if (!is.null(sre)) { 357 | yvals <- yvals + sre 358 | } 359 | if (!is.null(ire)) { 360 | yvals <- yvals + ire 361 | } 362 | cbind(tlists, Y = yvals, comb_mx) 363 | } 364 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: funfact 3 | --- 4 | 5 | funfact is an R package that makes it easier to plan, simulate, and analyze studies with factorial designs. It is intended for use with experiments in which multiple stimulus items are presented to multiple subjects. 6 | 7 | funfact can be used for the following purposes: 8 | 9 | - generating counterbalanced stimulus lists for any arbitrary factorial design; 10 | - generating a full list of trials corresponding to the realization of an experiment; 11 | - simulating data for any type of full factorial design; 12 | - generating a design formula for use in mixed model analysis in `lme4`. 13 | 14 | Each of these purposes is illustrated in one of the sections below. 15 | 16 | In addition, funfact provides convenience functions `contr.dev()` and `with_dev_pred()` for creating deviation-coded predictors for use in regression-based analyses of factorial designs. See http://talklab.psy.gla.ac.uk/tvw/catpred for discussion on coding schemes. 17 | 18 | ## funfact basics 19 | 20 | funfact conceptually separates the *design* of an experiment from the *realization* of that experiment through the sampling of subjects. 21 | 22 | The design of an experiment is represented using a list object with the following named elements: 23 | 24 | | Element Name | Description | 25 | |----------------+----------------------------------------------------------------------------------------------| 26 | | `ivs` | Names and levels of the independent variables (IVs) that form the main factors in the design | 27 | | `between_subj` | Names of any IVs administered between subjects | 28 | | `between_item` | Names of any IVs administered between items | 29 | | `n_item` | Number of stimulus items | 30 | | `n_rep` | Number of times each item should be presented to each participant (default 1) | 31 | 32 | For information on how these elements should be defined, see the examples in the next section. 33 | 34 | ## Generating counterbalanced stimulus presentation lists 35 | 36 | ```{r, echo = FALSE} 37 | library("funfact") 38 | ``` 39 | 40 | The function `stim_lists()` generates a set of counterbalanced stimulus presentation lists for any given (full) factorial design. A "stimulus presentation list" determines which items will be presented to a given subject, and in which condition. The generation process ensures that each subject sees the same number of items as well as the same number of items in each condition. It also ensures that each item is presented the same number of times, in each condition, across subjects. 41 | 42 | You determine the experiment design by specifying arguments within a list object. 43 | 44 | ### 2x2 design with both factors within-subjects and within-items 45 | 46 | ```{r} 47 | ## 2x2 within-subject within-item factorial design 48 | ## NB: here, the two IVs are A and B, each having 2 levels 49 | wswi_design <- list(ivs = c(A = 2, B = 2), 50 | n_item = 8) 51 | 52 | stim_lists(wswi_design) 53 | ``` 54 | 55 | ### A more complicated, two-way mixed design 56 | 57 | ```{r} 58 | mixed_design <- list(ivs = c(A = 2, B = 2), 59 | between_subj = c("A"), 60 | between_item = c("B"), 61 | n_item = 8) 62 | 63 | stim_lists(mixed_design) 64 | ``` 65 | 66 | ### 2x3 design, one factor between 67 | 68 | ```{r} 69 | ## note that IVs can also be specified as elements of a list, each 70 | ## key/value pair being the variable name and a vector with the levels 71 | ## of that variable 72 | bswi_2by3 <- list(ivs = list(Group = c("Child", "Adult"), 73 | Condition = c("Control", "High", "Low")), 74 | between_subj = c("Group"), 75 | n_item = 6) 76 | 77 | stim_lists(bswi_2by3) 78 | ``` 79 | 80 | ### 3-way mixed design 81 | 82 | The design below is a 3-way design based on Gann & Barr (2014). 83 | 84 | ```{r} 85 | threeway <- list(ivs = list(Novelty = c("New", "Old"), 86 | Addressee = c("Same", "Diff"), 87 | Feedback = c("Yes", "No")), 88 | between_subj = c("Addressee"), 89 | between_item = c("Feedback"), 90 | n_item = 16) 91 | 92 | stim_lists(threeway) 93 | ``` 94 | 95 | ## Generating trial lists 96 | 97 | While stimulus presentation lists determine which items a subject sees in which condition, trial lists randomly assign an equal number of subjects to each list. 98 | 99 | ```{r} 100 | wswi_design <- list(ivs = c(A = 2, B = 2), 101 | n_item = 8) 102 | 103 | trial_lists(wswi_design, subjects = 8) 104 | ``` 105 | 106 | ## Simulating data from a factorial design 107 | 108 | The functions `gen_pop()` and `sim_norm()` can be used to simulate data from any type of factorial design. 109 | 110 | `gen_pop()` is used to generate population parameters to be used in the simulation. `sim_norm()` takes a sample from the population defined by `gen_pop()`, as the example below illustrates. 111 | 112 | ```{r} 113 | my_design <- list(ivs = c(A = 2, B = 2), 114 | n_item = 8) 115 | popdata <- gen_pop(my_design, 8) 116 | 117 | my_data <- sim_norm(my_design, 8, popdata) 118 | my_data 119 | ``` 120 | 121 | ## Identifying the model formula to be used in mixed-effects modeling 122 | 123 | Simultaneous sampling of subjects and items can lead to complicated dependencies (see [Barr, Levy, Scheepers, and Tily, 2013](http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3881361)). One appealing way to account for these dependencies is through linear mixed-effects modeling, which can be accomplished using the lme4 package in R. The function =design_formula()= in funfact gives you the model formula that is appropriate for analyzing the data in your sample. 124 | 125 | ```{r} 126 | design_formula(threeway, n_subj = 16) 127 | ``` 128 | -------------------------------------------------------------------------------- /docs/LICENSE-text.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | License • funfact 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 45 | 46 | 47 | 48 | 49 | 50 |
51 |
52 | 87 | 88 | 89 |
90 | 91 |
92 |
93 | 96 | 97 |
                    GNU GENERAL PUBLIC LICENSE
 98 |                        Version 2, June 1991
 99 | 
100 |  Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/>
101 |  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
102 |  Everyone is permitted to copy and distribute verbatim copies
103 |  of this license document, but changing it is not allowed.
104 | 
105 |                             Preamble
106 | 
107 |   The licenses for most software are designed to take away your
108 | freedom to share and change it.  By contrast, the GNU General Public
109 | License is intended to guarantee your freedom to share and change free
110 | software--to make sure the software is free for all its users.  This
111 | General Public License applies to most of the Free Software
112 | Foundation's software and to any other program whose authors commit to
113 | using it.  (Some other Free Software Foundation software is covered by
114 | the GNU Lesser General Public License instead.)  You can apply it to
115 | your programs, too.
116 | 
117 |   When we speak of free software, we are referring to freedom, not
118 | price.  Our General Public Licenses are designed to make sure that you
119 | have the freedom to distribute copies of free software (and charge for
120 | this service if you wish), that you receive source code or can get it
121 | if you want it, that you can change the software or use pieces of it
122 | in new free programs; and that you know you can do these things.
123 | 
124 |   To protect your rights, we need to make restrictions that forbid
125 | anyone to deny you these rights or to ask you to surrender the rights.
126 | These restrictions translate to certain responsibilities for you if you
127 | distribute copies of the software, or if you modify it.
128 | 
129 |   For example, if you distribute copies of such a program, whether
130 | gratis or for a fee, you must give the recipients all the rights that
131 | you have.  You must make sure that they, too, receive or can get the
132 | source code.  And you must show them these terms so they know their
133 | rights.
134 | 
135 |   We protect your rights with two steps: (1) copyright the software, and
136 | (2) offer you this license which gives you legal permission to copy,
137 | distribute and/or modify the software.
138 | 
139 |   Also, for each author's protection and ours, we want to make certain
140 | that everyone understands that there is no warranty for this free
141 | software.  If the software is modified by someone else and passed on, we
142 | want its recipients to know that what they have is not the original, so
143 | that any problems introduced by others will not reflect on the original
144 | authors' reputations.
145 | 
146 |   Finally, any free program is threatened constantly by software
147 | patents.  We wish to avoid the danger that redistributors of a free
148 | program will individually obtain patent licenses, in effect making the
149 | program proprietary.  To prevent this, we have made it clear that any
150 | patent must be licensed for everyone's free use or not licensed at all.
151 | 
152 |   The precise terms and conditions for copying, distribution and
153 | modification follow.
154 | 
155 |                     GNU GENERAL PUBLIC LICENSE
156 |    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
157 | 
158 |   0. This License applies to any program or other work which contains
159 | a notice placed by the copyright holder saying it may be distributed
160 | under the terms of this General Public License.  The "Program", below,
161 | refers to any such program or work, and a "work based on the Program"
162 | means either the Program or any derivative work under copyright law:
163 | that is to say, a work containing the Program or a portion of it,
164 | either verbatim or with modifications and/or translated into another
165 | language.  (Hereinafter, translation is included without limitation in
166 | the term "modification".)  Each licensee is addressed as "you".
167 | 
168 | Activities other than copying, distribution and modification are not
169 | covered by this License; they are outside its scope.  The act of
170 | running the Program is not restricted, and the output from the Program
171 | is covered only if its contents constitute a work based on the
172 | Program (independent of having been made by running the Program).
173 | Whether that is true depends on what the Program does.
174 | 
175 |   1. You may copy and distribute verbatim copies of the Program's
176 | source code as you receive it, in any medium, provided that you
177 | conspicuously and appropriately publish on each copy an appropriate
178 | copyright notice and disclaimer of warranty; keep intact all the
179 | notices that refer to this License and to the absence of any warranty;
180 | and give any other recipients of the Program a copy of this License
181 | along with the Program.
182 | 
183 | You may charge a fee for the physical act of transferring a copy, and
184 | you may at your option offer warranty protection in exchange for a fee.
185 | 
186 |   2. You may modify your copy or copies of the Program or any portion
187 | of it, thus forming a work based on the Program, and copy and
188 | distribute such modifications or work under the terms of Section 1
189 | above, provided that you also meet all of these conditions:
190 | 
191 |     a) You must cause the modified files to carry prominent notices
192 |     stating that you changed the files and the date of any change.
193 | 
194 |     b) You must cause any work that you distribute or publish, that in
195 |     whole or in part contains or is derived from the Program or any
196 |     part thereof, to be licensed as a whole at no charge to all third
197 |     parties under the terms of this License.
198 | 
199 |     c) If the modified program normally reads commands interactively
200 |     when run, you must cause it, when started running for such
201 |     interactive use in the most ordinary way, to print or display an
202 |     announcement including an appropriate copyright notice and a
203 |     notice that there is no warranty (or else, saying that you provide
204 |     a warranty) and that users may redistribute the program under
205 |     these conditions, and telling the user how to view a copy of this
206 |     License.  (Exception: if the Program itself is interactive but
207 |     does not normally print such an announcement, your work based on
208 |     the Program is not required to print an announcement.)
209 | 
210 | These requirements apply to the modified work as a whole.  If
211 | identifiable sections of that work are not derived from the Program,
212 | and can be reasonably considered independent and separate works in
213 | themselves, then this License, and its terms, do not apply to those
214 | sections when you distribute them as separate works.  But when you
215 | distribute the same sections as part of a whole which is a work based
216 | on the Program, the distribution of the whole must be on the terms of
217 | this License, whose permissions for other licensees extend to the
218 | entire whole, and thus to each and every part regardless of who wrote it.
219 | 
220 | Thus, it is not the intent of this section to claim rights or contest
221 | your rights to work written entirely by you; rather, the intent is to
222 | exercise the right to control the distribution of derivative or
223 | collective works based on the Program.
224 | 
225 | In addition, mere aggregation of another work not based on the Program
226 | with the Program (or with a work based on the Program) on a volume of
227 | a storage or distribution medium does not bring the other work under
228 | the scope of this License.
229 | 
230 |   3. You may copy and distribute the Program (or a work based on it,
231 | under Section 2) in object code or executable form under the terms of
232 | Sections 1 and 2 above provided that you also do one of the following:
233 | 
234 |     a) Accompany it with the complete corresponding machine-readable
235 |     source code, which must be distributed under the terms of Sections
236 |     1 and 2 above on a medium customarily used for software interchange; or,
237 | 
238 |     b) Accompany it with a written offer, valid for at least three
239 |     years, to give any third party, for a charge no more than your
240 |     cost of physically performing source distribution, a complete
241 |     machine-readable copy of the corresponding source code, to be
242 |     distributed under the terms of Sections 1 and 2 above on a medium
243 |     customarily used for software interchange; or,
244 | 
245 |     c) Accompany it with the information you received as to the offer
246 |     to distribute corresponding source code.  (This alternative is
247 |     allowed only for noncommercial distribution and only if you
248 |     received the program in object code or executable form with such
249 |     an offer, in accord with Subsection b above.)
250 | 
251 | The source code for a work means the preferred form of the work for
252 | making modifications to it.  For an executable work, complete source
253 | code means all the source code for all modules it contains, plus any
254 | associated interface definition files, plus the scripts used to
255 | control compilation and installation of the executable.  However, as a
256 | special exception, the source code distributed need not include
257 | anything that is normally distributed (in either source or binary
258 | form) with the major components (compiler, kernel, and so on) of the
259 | operating system on which the executable runs, unless that component
260 | itself accompanies the executable.
261 | 
262 | If distribution of executable or object code is made by offering
263 | access to copy from a designated place, then offering equivalent
264 | access to copy the source code from the same place counts as
265 | distribution of the source code, even though third parties are not
266 | compelled to copy the source along with the object code.
267 | 
268 |   4. You may not copy, modify, sublicense, or distribute the Program
269 | except as expressly provided under this License.  Any attempt
270 | otherwise to copy, modify, sublicense or distribute the Program is
271 | void, and will automatically terminate your rights under this License.
272 | However, parties who have received copies, or rights, from you under
273 | this License will not have their licenses terminated so long as such
274 | parties remain in full compliance.
275 | 
276 |   5. You are not required to accept this License, since you have not
277 | signed it.  However, nothing else grants you permission to modify or
278 | distribute the Program or its derivative works.  These actions are
279 | prohibited by law if you do not accept this License.  Therefore, by
280 | modifying or distributing the Program (or any work based on the
281 | Program), you indicate your acceptance of this License to do so, and
282 | all its terms and conditions for copying, distributing or modifying
283 | the Program or works based on it.
284 | 
285 |   6. Each time you redistribute the Program (or any work based on the
286 | Program), the recipient automatically receives a license from the
287 | original licensor to copy, distribute or modify the Program subject to
288 | these terms and conditions.  You may not impose any further
289 | restrictions on the recipients' exercise of the rights granted herein.
290 | You are not responsible for enforcing compliance by third parties to
291 | this License.
292 | 
293 |   7. If, as a consequence of a court judgment or allegation of patent
294 | infringement or for any other reason (not limited to patent issues),
295 | conditions are imposed on you (whether by court order, agreement or
296 | otherwise) that contradict the conditions of this License, they do not
297 | excuse you from the conditions of this License.  If you cannot
298 | distribute so as to satisfy simultaneously your obligations under this
299 | License and any other pertinent obligations, then as a consequence you
300 | may not distribute the Program at all.  For example, if a patent
301 | license would not permit royalty-free redistribution of the Program by
302 | all those who receive copies directly or indirectly through you, then
303 | the only way you could satisfy both it and this License would be to
304 | refrain entirely from distribution of the Program.
305 | 
306 | If any portion of this section is held invalid or unenforceable under
307 | any particular circumstance, the balance of the section is intended to
308 | apply and the section as a whole is intended to apply in other
309 | circumstances.
310 | 
311 | It is not the purpose of this section to induce you to infringe any
312 | patents or other property right claims or to contest validity of any
313 | such claims; this section has the sole purpose of protecting the
314 | integrity of the free software distribution system, which is
315 | implemented by public license practices.  Many people have made
316 | generous contributions to the wide range of software distributed
317 | through that system in reliance on consistent application of that
318 | system; it is up to the author/donor to decide if he or she is willing
319 | to distribute software through any other system and a licensee cannot
320 | impose that choice.
321 | 
322 | This section is intended to make thoroughly clear what is believed to
323 | be a consequence of the rest of this License.
324 | 
325 |   8. If the distribution and/or use of the Program is restricted in
326 | certain countries either by patents or by copyrighted interfaces, the
327 | original copyright holder who places the Program under this License
328 | may add an explicit geographical distribution limitation excluding
329 | those countries, so that distribution is permitted only in or among
330 | countries not thus excluded.  In such case, this License incorporates
331 | the limitation as if written in the body of this License.
332 | 
333 |   9. The Free Software Foundation may publish revised and/or new versions
334 | of the General Public License from time to time.  Such new versions will
335 | be similar in spirit to the present version, but may differ in detail to
336 | address new problems or concerns.
337 | 
338 | Each version is given a distinguishing version number.  If the Program
339 | specifies a version number of this License which applies to it and "any
340 | later version", you have the option of following the terms and conditions
341 | either of that version or of any later version published by the Free
342 | Software Foundation.  If the Program does not specify a version number of
343 | this License, you may choose any version ever published by the Free Software
344 | Foundation.
345 | 
346 |   10. If you wish to incorporate parts of the Program into other free
347 | programs whose distribution conditions are different, write to the author
348 | to ask for permission.  For software which is copyrighted by the Free
349 | Software Foundation, write to the Free Software Foundation; we sometimes
350 | make exceptions for this.  Our decision will be guided by the two goals
351 | of preserving the free status of all derivatives of our free software and
352 | of promoting the sharing and reuse of software generally.
353 | 
354 |                             NO WARRANTY
355 | 
356 |   11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
357 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
358 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
359 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
360 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
361 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
362 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
363 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
364 | REPAIR OR CORRECTION.
365 | 
366 |   12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
367 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
368 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
369 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
370 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
371 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
372 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
373 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
374 | POSSIBILITY OF SUCH DAMAGES.
375 | 
376 |                      END OF TERMS AND CONDITIONS
377 | 
378 |             How to Apply These Terms to Your New Programs
379 | 
380 |   If you develop a new program, and you want it to be of the greatest
381 | possible use to the public, the best way to achieve this is to make it
382 | free software which everyone can redistribute and change under these terms.
383 | 
384 |   To do so, attach the following notices to the program.  It is safest
385 | to attach them to the start of each source file to most effectively
386 | convey the exclusion of warranty; and each file should have at least
387 | the "copyright" line and a pointer to where the full notice is found.
388 | 
389 |     {description}
390 |     Copyright (C) {year}  {fullname}
391 | 
392 |     This program is free software; you can redistribute it and/or modify
393 |     it under the terms of the GNU General Public License as published by
394 |     the Free Software Foundation; either version 2 of the License, or
395 |     (at your option) any later version.
396 | 
397 |     This program is distributed in the hope that it will be useful,
398 |     but WITHOUT ANY WARRANTY; without even the implied warranty of
399 |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
400 |     GNU General Public License for more details.
401 | 
402 |     You should have received a copy of the GNU General Public License along
403 |     with this program; if not, write to the Free Software Foundation, Inc.,
404 |     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
405 | 
406 | Also add information on how to contact you by electronic and paper mail.
407 | 
408 | If the program is interactive, make it output a short notice like this
409 | when it starts in an interactive mode:
410 | 
411 |     Gnomovision version 69, Copyright (C) year name of author
412 |     Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
413 |     This is free software, and you are welcome to redistribute it
414 |     under certain conditions; type `show c' for details.
415 | 
416 | The hypothetical commands `show w' and `show c' should show the appropriate
417 | parts of the General Public License.  Of course, the commands you use may
418 | be called something other than `show w' and `show c'; they could even be
419 | mouse-clicks or menu items--whatever suits your program.
420 | 
421 | You should also get your employer (if you work as a programmer) or your
422 | school, if any, to sign a "copyright disclaimer" for the program, if
423 | necessary.  Here is a sample; alter the names:
424 | 
425 |   Yoyodyne, Inc., hereby disclaims all copyright interest in the program
426 |   `Gnomovision' (which makes passes at compilers) written by James Hacker.
427 | 
428 |   {signature of Ty Coon}, 1 April 1989
429 |   Ty Coon, President of Vice
430 | 
431 | This General Public License does not permit incorporating your program into
432 | proprietary programs.  If your program is a subroutine library, you may
433 | consider it more useful to permit linking proprietary applications with the
434 | library.  If this is what you want to do, use the GNU Lesser General
435 | Public License instead of this License.
436 | 
437 | 438 |
439 | 440 |
441 | 442 | 443 |
444 | 447 | 448 |
449 |

Site built with pkgdown 1.3.0.

450 |
451 |
452 |
453 | 454 | 455 | 456 | 457 | 458 | 459 | -------------------------------------------------------------------------------- /docs/authors.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Authors • funfact 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 45 | 46 | 47 | 48 | 49 | 50 |
51 |
52 | 87 | 88 | 89 |
90 | 91 |
92 |
93 | 96 | 97 |
    98 |
  • 99 |

    Dale J. Barr. Maintainer. 100 |

    101 |
  • 102 |
103 | 104 |
105 | 106 |
107 | 108 | 109 |
110 | 113 | 114 |
115 |

Site built with pkgdown 1.3.0.

116 |
117 |
118 |
119 | 120 | 121 | 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- /docs/docsearch.css: -------------------------------------------------------------------------------- 1 | /* Docsearch -------------------------------------------------------------- */ 2 | /* 3 | Source: https://github.com/algolia/docsearch/ 4 | License: MIT 5 | */ 6 | 7 | .algolia-autocomplete { 8 | display: block; 9 | -webkit-box-flex: 1; 10 | -ms-flex: 1; 11 | flex: 1 12 | } 13 | 14 | .algolia-autocomplete .ds-dropdown-menu { 15 | width: 100%; 16 | min-width: none; 17 | max-width: none; 18 | padding: .75rem 0; 19 | background-color: #fff; 20 | background-clip: padding-box; 21 | border: 1px solid rgba(0, 0, 0, .1); 22 | box-shadow: 0 .5rem 1rem rgba(0, 0, 0, .175); 23 | } 24 | 25 | @media (min-width:768px) { 26 | .algolia-autocomplete .ds-dropdown-menu { 27 | width: 175% 28 | } 29 | } 30 | 31 | .algolia-autocomplete .ds-dropdown-menu::before { 32 | display: none 33 | } 34 | 35 | .algolia-autocomplete .ds-dropdown-menu [class^=ds-dataset-] { 36 | padding: 0; 37 | background-color: rgb(255,255,255); 38 | border: 0; 39 | max-height: 80vh; 40 | } 41 | 42 | .algolia-autocomplete .ds-dropdown-menu .ds-suggestions { 43 | margin-top: 0 44 | } 45 | 46 | .algolia-autocomplete .algolia-docsearch-suggestion { 47 | padding: 0; 48 | overflow: visible 49 | } 50 | 51 | .algolia-autocomplete .algolia-docsearch-suggestion--category-header { 52 | padding: .125rem 1rem; 53 | margin-top: 0; 54 | font-size: 1.3em; 55 | font-weight: 500; 56 | color: #00008B; 57 | border-bottom: 0 58 | } 59 | 60 | .algolia-autocomplete .algolia-docsearch-suggestion--wrapper { 61 | float: none; 62 | padding-top: 0 63 | } 64 | 65 | .algolia-autocomplete .algolia-docsearch-suggestion--subcategory-column { 66 | float: none; 67 | width: auto; 68 | padding: 0; 69 | text-align: left 70 | } 71 | 72 | .algolia-autocomplete .algolia-docsearch-suggestion--content { 73 | float: none; 74 | width: auto; 75 | padding: 0 76 | } 77 | 78 | .algolia-autocomplete .algolia-docsearch-suggestion--content::before { 79 | display: none 80 | } 81 | 82 | .algolia-autocomplete .ds-suggestion:not(:first-child) .algolia-docsearch-suggestion--category-header { 83 | padding-top: .75rem; 84 | margin-top: .75rem; 85 | border-top: 1px solid rgba(0, 0, 0, .1) 86 | } 87 | 88 | .algolia-autocomplete .ds-suggestion .algolia-docsearch-suggestion--subcategory-column { 89 | display: block; 90 | padding: .1rem 1rem; 91 | margin-bottom: 0.1; 92 | font-size: 1.0em; 93 | font-weight: 400 94 | /* display: none */ 95 | } 96 | 97 | .algolia-autocomplete .algolia-docsearch-suggestion--title { 98 | display: block; 99 | padding: .25rem 1rem; 100 | margin-bottom: 0; 101 | font-size: 0.9em; 102 | font-weight: 400 103 | } 104 | 105 | .algolia-autocomplete .algolia-docsearch-suggestion--text { 106 | padding: 0 1rem .5rem; 107 | margin-top: -.25rem; 108 | font-size: 0.8em; 109 | font-weight: 400; 110 | line-height: 1.25 111 | } 112 | 113 | .algolia-autocomplete .algolia-docsearch-footer { 114 | width: 110px; 115 | height: 20px; 116 | z-index: 3; 117 | margin-top: 10.66667px; 118 | float: right; 119 | font-size: 0; 120 | line-height: 0; 121 | } 122 | 123 | .algolia-autocomplete .algolia-docsearch-footer--logo { 124 | background-image: url("data:image/svg+xml;utf8,"); 125 | background-repeat: no-repeat; 126 | background-position: 50%; 127 | background-size: 100%; 128 | overflow: hidden; 129 | text-indent: -9000px; 130 | width: 100%; 131 | height: 100%; 132 | display: block; 133 | transform: translate(-8px); 134 | } 135 | 136 | .algolia-autocomplete .algolia-docsearch-suggestion--highlight { 137 | color: #FF8C00; 138 | background: rgba(232, 189, 54, 0.1) 139 | } 140 | 141 | 142 | .algolia-autocomplete .algolia-docsearch-suggestion--text .algolia-docsearch-suggestion--highlight { 143 | box-shadow: inset 0 -2px 0 0 rgba(105, 105, 105, .5) 144 | } 145 | 146 | .algolia-autocomplete .ds-suggestion.ds-cursor .algolia-docsearch-suggestion--content { 147 | background-color: rgba(192, 192, 192, .15) 148 | } 149 | -------------------------------------------------------------------------------- /docs/docsearch.js: -------------------------------------------------------------------------------- 1 | $(function() { 2 | 3 | // register a handler to move the focus to the search bar 4 | // upon pressing shift + "/" (i.e. "?") 5 | $(document).on('keydown', function(e) { 6 | if (e.shiftKey && e.keyCode == 191) { 7 | e.preventDefault(); 8 | $("#search-input").focus(); 9 | } 10 | }); 11 | 12 | $(document).ready(function() { 13 | // do keyword highlighting 14 | /* modified from https://jsfiddle.net/julmot/bL6bb5oo/ */ 15 | var mark = function() { 16 | 17 | var referrer = document.URL ; 18 | var paramKey = "q" ; 19 | 20 | if (referrer.indexOf("?") !== -1) { 21 | var qs = referrer.substr(referrer.indexOf('?') + 1); 22 | var qs_noanchor = qs.split('#')[0]; 23 | var qsa = qs_noanchor.split('&'); 24 | var keyword = ""; 25 | 26 | for (var i = 0; i < qsa.length; i++) { 27 | var currentParam = qsa[i].split('='); 28 | 29 | if (currentParam.length !== 2) { 30 | continue; 31 | } 32 | 33 | if (currentParam[0] == paramKey) { 34 | keyword = decodeURIComponent(currentParam[1].replace(/\+/g, "%20")); 35 | } 36 | } 37 | 38 | if (keyword !== "") { 39 | $(".contents").unmark({ 40 | done: function() { 41 | $(".contents").mark(keyword); 42 | } 43 | }); 44 | } 45 | } 46 | }; 47 | 48 | mark(); 49 | }); 50 | }); 51 | 52 | /* Search term highlighting ------------------------------*/ 53 | 54 | function matchedWords(hit) { 55 | var words = []; 56 | 57 | var hierarchy = hit._highlightResult.hierarchy; 58 | // loop to fetch from lvl0, lvl1, etc. 59 | for (var idx in hierarchy) { 60 | words = words.concat(hierarchy[idx].matchedWords); 61 | } 62 | 63 | var content = hit._highlightResult.content; 64 | if (content) { 65 | words = words.concat(content.matchedWords); 66 | } 67 | 68 | // return unique words 69 | var words_uniq = [...new Set(words)]; 70 | return words_uniq; 71 | } 72 | 73 | function updateHitURL(hit) { 74 | 75 | var words = matchedWords(hit); 76 | var url = ""; 77 | 78 | if (hit.anchor) { 79 | url = hit.url_without_anchor + '?q=' + escape(words.join(" ")) + '#' + hit.anchor; 80 | } else { 81 | url = hit.url + '?q=' + escape(words.join(" ")); 82 | } 83 | 84 | return url; 85 | } 86 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Functions for factorial experiments • funfact 9 | 10 | 11 | 12 | 13 | 16 | 17 | 21 | 22 | 23 |
24 |
57 | 58 | 59 | 60 |
61 |
62 | 63 | 64 | 65 | 66 |

funfact is an R package that makes it easier to plan, simulate, and analyze studies with factorial designs. It is intended for use with experiments in which multiple stimulus items are presented to multiple subjects.

67 |

funfact can be used for the following purposes:

68 |
    69 |
  • generating counterbalanced stimulus lists for any arbitrary factorial design;
  • 70 |
  • generating a full list of trials corresponding to the realization of an experiment;
  • 71 |
  • simulating data for any type of full factorial design;
  • 72 |
  • generating a design formula for use in mixed model analysis in lme4.
  • 73 |
74 |

Each of these purposes is illustrated in one of the sections below.

75 |

In addition, funfact provides convenience functions contr.dev() and with_dev_pred() for creating deviation-coded predictors for use in regression-based analyses of factorial designs. See http://talklab.psy.gla.ac.uk/tvw/catpred for discussion on coding schemes.

76 |
77 |

78 | funfact basics

79 |

funfact conceptually separates the design of an experiment from the realization of that experiment through the sampling of subjects.

80 |

The design of an experiment is represented using a list object with the following named elements:

81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 |
Element NameDescription
ivsNames and levels of the independent variables (IVs) that form the main factors in the design
between_subjNames of any IVs administered between subjects
between_itemNames of any IVs administered between items
n_itemNumber of stimulus items
n_repNumber of times each item should be presented to each participant (default 1)
113 |

For information on how these elements should be defined, see the examples in the next section.

114 |
115 |
116 |

117 | Generating counterbalanced stimulus presentation lists

118 |

The function stim_lists() generates a set of counterbalanced stimulus presentation lists for any given (full) factorial design. A “stimulus presentation list” determines which items will be presented to a given subject, and in which condition. The generation process ensures that each subject sees the same number of items as well as the same number of items in each condition. It also ensures that each item is presented the same number of times, in each condition, across subjects.

119 |

You determine the experiment design by specifying arguments within a list object.

120 |
121 |

122 | 2x2 design with both factors within-subjects and within-items

123 |
  ## 2x2 within-subject within-item factorial design
124 |   ## NB: here, the two IVs are A and B, each having 2 levels
125 |   wswi_design <- list(ivs = c(A = 2, B = 2),
126 |                       n_item = 8)
127 | 
128 |   stim_lists(wswi_design)
129 |
##    list_id item_id  A  B
130 | ## 1        1       1 A1 B1
131 | ## 2        1       2 A1 B1
132 | ## 3        1       3 A1 B2
133 | ## 4        1       4 A1 B2
134 | ## 5        1       5 A2 B1
135 | ## 6        1       6 A2 B1
136 | ## 7        1       7 A2 B2
137 | ## 8        1       8 A2 B2
138 | ## 9        2       1 A1 B2
139 | ## 10       2       2 A1 B2
140 | ## 11       2       3 A2 B1
141 | ## 12       2       4 A2 B1
142 | ## 13       2       5 A2 B2
143 | ## 14       2       6 A2 B2
144 | ## 15       2       7 A1 B1
145 | ## 16       2       8 A1 B1
146 | ## 17       3       1 A2 B1
147 | ## 18       3       2 A2 B1
148 | ## 19       3       3 A2 B2
149 | ## 20       3       4 A2 B2
150 | ## 21       3       5 A1 B1
151 | ## 22       3       6 A1 B1
152 | ## 23       3       7 A1 B2
153 | ## 24       3       8 A1 B2
154 | ## 25       4       1 A2 B2
155 | ## 26       4       2 A2 B2
156 | ## 27       4       3 A1 B1
157 | ## 28       4       4 A1 B1
158 | ## 29       4       5 A1 B2
159 | ## 30       4       6 A1 B2
160 | ## 31       4       7 A2 B1
161 | ## 32       4       8 A2 B1
162 |
163 |
164 |

165 | A more complicated, two-way mixed design

166 |
  mixed_design <- list(ivs = c(A = 2, B = 2),
167 |                        between_subj = c("A"),
168 |                        between_item = c("B"),
169 |                        n_item = 8)
170 | 
171 |   stim_lists(mixed_design)
172 |
##    list_id item_id  A  B
173 | ## 1        1       1 A1 B1
174 | ## 2        1       2 A1 B1
175 | ## 3        1       3 A1 B1
176 | ## 4        1       4 A1 B1
177 | ## 5        1       5 A1 B2
178 | ## 6        1       6 A1 B2
179 | ## 7        1       7 A1 B2
180 | ## 8        1       8 A1 B2
181 | ## 9        2       1 A2 B1
182 | ## 10       2       2 A2 B1
183 | ## 11       2       3 A2 B1
184 | ## 12       2       4 A2 B1
185 | ## 13       2       5 A2 B2
186 | ## 14       2       6 A2 B2
187 | ## 15       2       7 A2 B2
188 | ## 16       2       8 A2 B2
189 |
190 |
191 |

192 | 2x3 design, one factor between

193 |
  ## note that IVs can also be specified as elements of a list, each
194 |   ## key/value pair being the variable name and a vector with the levels
195 |   ## of that variable
196 |   bswi_2by3 <- list(ivs = list(Group = c("Child", "Adult"),
197 |                         Condition = c("Control", "High", "Low")),
198 |                     between_subj = c("Group"),
199 |                     n_item = 6)
200 | 
201 |   stim_lists(bswi_2by3)
202 |
##    list_id item_id Group Condition
203 | ## 1        1       1 Child   Control
204 | ## 2        1       2 Child   Control
205 | ## 3        1       3 Child      High
206 | ## 4        1       4 Child      High
207 | ## 5        1       5 Child       Low
208 | ## 6        1       6 Child       Low
209 | ## 7        2       1 Child      High
210 | ## 8        2       2 Child      High
211 | ## 9        2       3 Child       Low
212 | ## 10       2       4 Child       Low
213 | ## 11       2       5 Child   Control
214 | ## 12       2       6 Child   Control
215 | ## 13       3       1 Child       Low
216 | ## 14       3       2 Child       Low
217 | ## 15       3       3 Child   Control
218 | ## 16       3       4 Child   Control
219 | ## 17       3       5 Child      High
220 | ## 18       3       6 Child      High
221 | ## 19       4       1 Adult   Control
222 | ## 20       4       2 Adult   Control
223 | ## 21       4       3 Adult      High
224 | ## 22       4       4 Adult      High
225 | ## 23       4       5 Adult       Low
226 | ## 24       4       6 Adult       Low
227 | ## 25       5       1 Adult      High
228 | ## 26       5       2 Adult      High
229 | ## 27       5       3 Adult       Low
230 | ## 28       5       4 Adult       Low
231 | ## 29       5       5 Adult   Control
232 | ## 30       5       6 Adult   Control
233 | ## 31       6       1 Adult       Low
234 | ## 32       6       2 Adult       Low
235 | ## 33       6       3 Adult   Control
236 | ## 34       6       4 Adult   Control
237 | ## 35       6       5 Adult      High
238 | ## 36       6       6 Adult      High
239 |
240 |
241 |

242 | 3-way mixed design

243 |

The design below is a 3-way design based on Gann & Barr (2014).

244 |
  threeway <- list(ivs = list(Novelty = c("New", "Old"),
245 |                        Addressee = c("Same", "Diff"),
246 |                        Feedback = c("Yes", "No")),
247 |                    between_subj = c("Addressee"),
248 |                    between_item = c("Feedback"),
249 |                    n_item = 16)
250 | 
251 |   stim_lists(threeway)
252 |
##    list_id item_id Addressee Feedback Novelty
253 | ## 1        1       1      Same      Yes     New
254 | ## 2        1       2      Same      Yes     New
255 | ## 3        1       3      Same      Yes     New
256 | ## 4        1       4      Same      Yes     New
257 | ## 5        1       5      Same      Yes     Old
258 | ## 6        1       6      Same      Yes     Old
259 | ## 7        1       7      Same      Yes     Old
260 | ## 8        1       8      Same      Yes     Old
261 | ## 9        1       9      Same       No     New
262 | ## 10       1      10      Same       No     New
263 | ## 11       1      11      Same       No     New
264 | ## 12       1      12      Same       No     New
265 | ## 13       1      13      Same       No     Old
266 | ## 14       1      14      Same       No     Old
267 | ## 15       1      15      Same       No     Old
268 | ## 16       1      16      Same       No     Old
269 | ## 17       2       1      Same      Yes     Old
270 | ## 18       2       2      Same      Yes     Old
271 | ## 19       2       3      Same      Yes     Old
272 | ## 20       2       4      Same      Yes     Old
273 | ## 21       2       5      Same      Yes     New
274 | ## 22       2       6      Same      Yes     New
275 | ## 23       2       7      Same      Yes     New
276 | ## 24       2       8      Same      Yes     New
277 | ## 25       2       9      Same       No     Old
278 | ## 26       2      10      Same       No     Old
279 | ## 27       2      11      Same       No     Old
280 | ## 28       2      12      Same       No     Old
281 | ## 29       2      13      Same       No     New
282 | ## 30       2      14      Same       No     New
283 | ## 31       2      15      Same       No     New
284 | ## 32       2      16      Same       No     New
285 | ## 33       3       1      Diff      Yes     New
286 | ## 34       3       2      Diff      Yes     New
287 | ## 35       3       3      Diff      Yes     New
288 | ## 36       3       4      Diff      Yes     New
289 | ## 37       3       5      Diff      Yes     Old
290 | ## 38       3       6      Diff      Yes     Old
291 | ## 39       3       7      Diff      Yes     Old
292 | ## 40       3       8      Diff      Yes     Old
293 | ## 41       3       9      Diff       No     New
294 | ## 42       3      10      Diff       No     New
295 | ## 43       3      11      Diff       No     New
296 | ## 44       3      12      Diff       No     New
297 | ## 45       3      13      Diff       No     Old
298 | ## 46       3      14      Diff       No     Old
299 | ## 47       3      15      Diff       No     Old
300 | ## 48       3      16      Diff       No     Old
301 | ## 49       4       1      Diff      Yes     Old
302 | ## 50       4       2      Diff      Yes     Old
303 | ## 51       4       3      Diff      Yes     Old
304 | ## 52       4       4      Diff      Yes     Old
305 | ## 53       4       5      Diff      Yes     New
306 | ## 54       4       6      Diff      Yes     New
307 | ## 55       4       7      Diff      Yes     New
308 | ## 56       4       8      Diff      Yes     New
309 | ## 57       4       9      Diff       No     Old
310 | ## 58       4      10      Diff       No     Old
311 | ## 59       4      11      Diff       No     Old
312 | ## 60       4      12      Diff       No     Old
313 | ## 61       4      13      Diff       No     New
314 | ## 62       4      14      Diff       No     New
315 | ## 63       4      15      Diff       No     New
316 | ## 64       4      16      Diff       No     New
317 |
318 |
319 |
320 |

321 | Generating trial lists

322 |

While stimulus presentation lists determine which items a subject sees in which condition, trial lists randomly assign an equal number of subjects to each list.

323 |
  wswi_design <- list(ivs = c(A = 2, B = 2),
324 |                       n_item = 8)
325 | 
326 |   trial_lists(wswi_design, subjects = 8)
327 |
##    subj_id list_id item_id  A  B
328 | ## 1        1       3       1 A2 B1
329 | ## 2        1       3       2 A2 B1
330 | ## 3        1       3       3 A2 B2
331 | ## 4        1       3       4 A2 B2
332 | ## 5        1       3       5 A1 B1
333 | ## 6        1       3       6 A1 B1
334 | ## 7        1       3       7 A1 B2
335 | ## 8        1       3       8 A1 B2
336 | ## 9        2       4       1 A2 B2
337 | ## 10       2       4       2 A2 B2
338 | ## 11       2       4       3 A1 B1
339 | ## 12       2       4       4 A1 B1
340 | ## 13       2       4       5 A1 B2
341 | ## 14       2       4       6 A1 B2
342 | ## 15       2       4       7 A2 B1
343 | ## 16       2       4       8 A2 B1
344 | ## 17       3       1       1 A1 B1
345 | ## 18       3       1       2 A1 B1
346 | ## 19       3       1       3 A1 B2
347 | ## 20       3       1       4 A1 B2
348 | ## 21       3       1       5 A2 B1
349 | ## 22       3       1       6 A2 B1
350 | ## 23       3       1       7 A2 B2
351 | ## 24       3       1       8 A2 B2
352 | ## 25       4       4       1 A2 B2
353 | ## 26       4       4       2 A2 B2
354 | ## 27       4       4       3 A1 B1
355 | ## 28       4       4       4 A1 B1
356 | ## 29       4       4       5 A1 B2
357 | ## 30       4       4       6 A1 B2
358 | ## 31       4       4       7 A2 B1
359 | ## 32       4       4       8 A2 B1
360 | ## 33       5       2       1 A1 B2
361 | ## 34       5       2       2 A1 B2
362 | ## 35       5       2       3 A2 B1
363 | ## 36       5       2       4 A2 B1
364 | ## 37       5       2       5 A2 B2
365 | ## 38       5       2       6 A2 B2
366 | ## 39       5       2       7 A1 B1
367 | ## 40       5       2       8 A1 B1
368 | ## 41       6       1       1 A1 B1
369 | ## 42       6       1       2 A1 B1
370 | ## 43       6       1       3 A1 B2
371 | ## 44       6       1       4 A1 B2
372 | ## 45       6       1       5 A2 B1
373 | ## 46       6       1       6 A2 B1
374 | ## 47       6       1       7 A2 B2
375 | ## 48       6       1       8 A2 B2
376 | ## 49       7       3       1 A2 B1
377 | ## 50       7       3       2 A2 B1
378 | ## 51       7       3       3 A2 B2
379 | ## 52       7       3       4 A2 B2
380 | ## 53       7       3       5 A1 B1
381 | ## 54       7       3       6 A1 B1
382 | ## 55       7       3       7 A1 B2
383 | ## 56       7       3       8 A1 B2
384 | ## 57       8       2       1 A1 B2
385 | ## 58       8       2       2 A1 B2
386 | ## 59       8       2       3 A2 B1
387 | ## 60       8       2       4 A2 B1
388 | ## 61       8       2       5 A2 B2
389 | ## 62       8       2       6 A2 B2
390 | ## 63       8       2       7 A1 B1
391 | ## 64       8       2       8 A1 B1
392 |
393 |
394 |

395 | Simulating data from a factorial design

396 |

The functions gen_pop() and sim_norm() can be used to simulate data from any type of factorial design.

397 |

gen_pop() is used to generate population parameters to be used in the simulation. sim_norm() takes a sample from the population defined by gen_pop(), as the example below illustrates.

398 |
  my_design <- list(ivs = c(A = 2, B = 2),
399 |                     n_item = 8)
400 |   popdata <- gen_pop(my_design, 8)
401 | 
402 |   my_data <- sim_norm(my_design, 8, popdata)
403 |   my_data
404 |
##    subj_id list_id item_id  A  B           Y         err
405 | ## 1        1       4       1 A2 B2  5.01689929  0.87842477
406 | ## 2        1       4       2 A2 B2  4.38878926  0.54911911
407 | ## 3        1       4       3 A1 B1  1.11699093  1.13036926
408 | ## 4        1       4       4 A1 B1 -1.37261803 -0.75655038
409 | ## 5        1       4       5 A1 B2  3.08824910 -0.54462380
410 | ## 6        1       4       6 A1 B2  3.16408362  0.52654105
411 | ## 7        1       4       7 A2 B1  1.43523189 -0.60861942
412 | ## 8        1       4       8 A2 B1 -2.96707125 -1.08123639
413 | ## 9        2       4       1 A2 B2  3.62682046  1.82008435
414 | ## 10       2       4       2 A2 B2  1.66457951  0.15664777
415 | ## 11       2       4       3 A1 B1 -4.12997003 -3.07323658
416 | ## 12       2       4       4 A1 B1 -0.06318779  1.59623499
417 | ## 13       2       4       5 A1 B2  5.54570583  1.55729875
418 | ## 14       2       4       6 A1 B2  6.23910322  3.24602648
419 | ## 15       2       4       7 A2 B1  1.96767277  0.35720436
420 | ## 16       2       4       8 A2 B1 -3.95741355 -1.63819578
421 | ## 17       3       2       1 A1 B2  0.62310726  0.18186913
422 | ## 18       3       2       2 A1 B2  2.84852317  2.42995361
423 | ## 19       3       2       3 A2 B1  2.93416550  2.84802105
424 | ## 20       3       2       4 A2 B1 -1.86441745 -2.21153506
425 | ## 21       3       2       5 A2 B2  5.54234548  1.62438411
426 | ## 22       3       2       6 A2 B2  4.34874446  1.19800313
427 | ## 23       3       2       7 A1 B1  0.99181306  1.07747370
428 | ## 24       3       2       8 A1 B1 -5.07793578 -1.44868782
429 | ## 25       4       2       1 A1 B2 -1.86092555 -3.77503400
430 | ## 26       4       2       2 A1 B2  1.05830819 -0.83313170
431 | ## 27       4       2       3 A2 B1 -1.72501882 -2.27181093
432 | ## 28       4       2       4 A2 B1  2.15651698  1.34875170
433 | ## 29       4       2       5 A2 B2  6.51251600  1.50067182
434 | ## 30       4       2       6 A2 B2  7.21721951  2.97259536
435 | ## 31       4       2       7 A1 B1 -0.66632710 -2.33294398
436 | ## 32       4       2       8 A1 B1 -4.88096622 -3.00399578
437 | ## 33       5       1       1 A1 B1 -1.78701801 -0.38607379
438 | ## 34       5       1       2 A1 B1 -2.34810557 -2.26404305
439 | ## 35       5       1       3 A1 B2 -1.01401176 -4.68876659
440 | ## 36       5       1       4 A1 B2  5.26411754  2.63121706
441 | ## 37       5       1       5 A2 B1 -0.95034358  0.17980574
442 | ## 38       5       1       6 A2 B1  1.44786141  1.30697374
443 | ## 39       5       1       7 A2 B2  5.13209663  0.84701676
444 | ## 40       5       1       8 A2 B2  1.61319128 -2.89352793
445 | ## 41       6       3       1 A2 B1  0.71193156  0.33470335
446 | ## 42       6       3       2 A2 B1 -0.98627644 -2.07839456
447 | ## 43       6       3       3 A2 B2  1.45981679 -1.15834951
448 | ## 44       6       3       4 A2 B2  4.47707391  0.47322822
449 | ## 45       6       3       5 A1 B1 -0.03030409  1.12360400
450 | ## 46       6       3       6 A1 B1  0.02438670  0.54991336
451 | ## 47       6       3       7 A1 B2  1.02196468 -0.09761682
452 | ## 48       6       3       8 A1 B2 -1.62145736 -3.56747166
453 | ## 49       7       1       1 A1 B1 -1.19913187  0.69500964
454 | ## 50       7       1       2 A1 B1 -0.14203225  0.43522756
455 | ## 51       7       1       3 A1 B2  2.78339614 -0.36544194
456 | ## 52       7       1       4 A1 B2 -0.46099038 -2.56797412
457 | ## 53       7       1       5 A2 B1  3.21020113  2.83079355
458 | ## 54       7       1       6 A2 B1  3.64246520  1.99202064
459 | ## 55       7       1       7 A2 B2  9.84260173  3.11607461
460 | ## 56       7       1       8 A2 B2  6.02346797 -0.92469846
461 | ## 57       8       3       1 A2 B1  1.50088558  0.30064124
462 | ## 58       8       3       2 A2 B1  4.98827729  3.07314303
463 | ## 59       8       3       3 A2 B2  4.57124615  0.74551922
464 | ## 60       8       3       4 A2 B2  2.90029999 -2.31110632
465 | ## 61       8       3       5 A1 B1 -4.80927467  0.13641578
466 | ## 62       8       3       6 A1 B1 -6.30242151 -1.98511248
467 | ## 63       8       3       7 A1 B2  2.00756103  4.04041129
468 | ## 64       8       3       8 A1 B2 -0.98576259  0.22065487
469 |
470 |
471 |

472 | Identifying the model formula to be used in mixed-effects modeling

473 |

Simultaneous sampling of subjects and items can lead to complicated dependencies (see Barr, Levy, Scheepers, and Tily, 2013). One appealing way to account for these dependencies is through linear mixed-effects modeling, which can be accomplished using the lme4 package in R. The function =design_formula()= in funfact gives you the model formula that is appropriate for analyzing the data in your sample.

474 |
design_formula(threeway, n_subj = 16)
475 |
## ~Novelty * Addressee * Feedback + (1 + Novelty * Feedback | subj_id) + 
476 | ##     (1 + Novelty * Addressee | item_id)
477 | ## <environment: 0x55b8eac92e60>
478 |
479 |
480 | 481 | 496 | 497 |
498 | 499 | 500 |
503 | 504 |
505 |

Site built with pkgdown 1.3.0.

506 |
507 |
508 |
509 | 510 | 511 | 512 | 513 | 514 | -------------------------------------------------------------------------------- /docs/link.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 8 | 12 | 13 | -------------------------------------------------------------------------------- /docs/pkgdown.css: -------------------------------------------------------------------------------- 1 | /* Sticky footer */ 2 | 3 | /** 4 | * Basic idea: https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/ 5 | * Details: https://github.com/philipwalton/solved-by-flexbox/blob/master/assets/css/components/site.css 6 | * 7 | * .Site -> body > .container 8 | * .Site-content -> body > .container .row 9 | * .footer -> footer 10 | * 11 | * Key idea seems to be to ensure that .container and __all its parents__ 12 | * have height set to 100% 13 | * 14 | */ 15 | 16 | html, body { 17 | height: 100%; 18 | } 19 | 20 | body > .container { 21 | display: flex; 22 | height: 100%; 23 | flex-direction: column; 24 | 25 | padding-top: 60px; 26 | } 27 | 28 | body > .container .row { 29 | flex: 1 0 auto; 30 | } 31 | 32 | footer { 33 | margin-top: 45px; 34 | padding: 35px 0 36px; 35 | border-top: 1px solid #e5e5e5; 36 | color: #666; 37 | display: flex; 38 | flex-shrink: 0; 39 | } 40 | footer p { 41 | margin-bottom: 0; 42 | } 43 | footer div { 44 | flex: 1; 45 | } 46 | footer .pkgdown { 47 | text-align: right; 48 | } 49 | footer p { 50 | margin-bottom: 0; 51 | } 52 | 53 | img.icon { 54 | float: right; 55 | } 56 | 57 | img { 58 | max-width: 100%; 59 | } 60 | 61 | /* Fix bug in bootstrap (only seen in firefox) */ 62 | summary { 63 | display: list-item; 64 | } 65 | 66 | /* Typographic tweaking ---------------------------------*/ 67 | 68 | .contents .page-header { 69 | margin-top: calc(-60px + 1em); 70 | } 71 | 72 | /* Section anchors ---------------------------------*/ 73 | 74 | a.anchor { 75 | margin-left: -30px; 76 | display:inline-block; 77 | width: 30px; 78 | height: 30px; 79 | visibility: hidden; 80 | 81 | background-image: url(./link.svg); 82 | background-repeat: no-repeat; 83 | background-size: 20px 20px; 84 | background-position: center center; 85 | } 86 | 87 | .hasAnchor:hover a.anchor { 88 | visibility: visible; 89 | } 90 | 91 | @media (max-width: 767px) { 92 | .hasAnchor:hover a.anchor { 93 | visibility: hidden; 94 | } 95 | } 96 | 97 | 98 | /* Fixes for fixed navbar --------------------------*/ 99 | 100 | .contents h1, .contents h2, .contents h3, .contents h4 { 101 | padding-top: 60px; 102 | margin-top: -40px; 103 | } 104 | 105 | /* Static header placement on mobile devices */ 106 | @media (max-width: 767px) { 107 | .navbar-fixed-top { 108 | position: absolute; 109 | } 110 | .navbar { 111 | padding: 0; 112 | } 113 | } 114 | 115 | 116 | /* Sidebar --------------------------*/ 117 | 118 | #sidebar { 119 | margin-top: 30px; 120 | } 121 | #sidebar h2 { 122 | font-size: 1.5em; 123 | margin-top: 1em; 124 | } 125 | 126 | #sidebar h2:first-child { 127 | margin-top: 0; 128 | } 129 | 130 | #sidebar .list-unstyled li { 131 | margin-bottom: 0.5em; 132 | } 133 | 134 | .orcid { 135 | height: 16px; 136 | vertical-align: middle; 137 | } 138 | 139 | /* Reference index & topics ----------------------------------------------- */ 140 | 141 | .ref-index th {font-weight: normal;} 142 | 143 | .ref-index td {vertical-align: top;} 144 | .ref-index .icon {width: 40px;} 145 | .ref-index .alias {width: 40%;} 146 | .ref-index-icons .alias {width: calc(40% - 40px);} 147 | .ref-index .title {width: 60%;} 148 | 149 | .ref-arguments th {text-align: right; padding-right: 10px;} 150 | .ref-arguments th, .ref-arguments td {vertical-align: top;} 151 | .ref-arguments .name {width: 20%;} 152 | .ref-arguments .desc {width: 80%;} 153 | 154 | /* Nice scrolling for wide elements --------------------------------------- */ 155 | 156 | table { 157 | display: block; 158 | overflow: auto; 159 | } 160 | 161 | /* Syntax highlighting ---------------------------------------------------- */ 162 | 163 | pre { 164 | word-wrap: normal; 165 | word-break: normal; 166 | border: 1px solid #eee; 167 | } 168 | 169 | pre, code { 170 | background-color: #f8f8f8; 171 | color: #333; 172 | } 173 | 174 | pre code { 175 | overflow: auto; 176 | word-wrap: normal; 177 | white-space: pre; 178 | } 179 | 180 | pre .img { 181 | margin: 5px 0; 182 | } 183 | 184 | pre .img img { 185 | background-color: #fff; 186 | display: block; 187 | height: auto; 188 | } 189 | 190 | code a, pre a { 191 | color: #375f84; 192 | } 193 | 194 | a.sourceLine:hover { 195 | text-decoration: none; 196 | } 197 | 198 | .fl {color: #1514b5;} 199 | .fu {color: #000000;} /* function */ 200 | .ch,.st {color: #036a07;} /* string */ 201 | .kw {color: #264D66;} /* keyword */ 202 | .co {color: #888888;} /* comment */ 203 | 204 | .message { color: black; font-weight: bolder;} 205 | .error { color: orange; font-weight: bolder;} 206 | .warning { color: #6A0366; font-weight: bolder;} 207 | 208 | /* Clipboard --------------------------*/ 209 | 210 | .hasCopyButton { 211 | position: relative; 212 | } 213 | 214 | .btn-copy-ex { 215 | position: absolute; 216 | right: 0; 217 | top: 0; 218 | visibility: hidden; 219 | } 220 | 221 | .hasCopyButton:hover button.btn-copy-ex { 222 | visibility: visible; 223 | } 224 | 225 | /* mark.js ----------------------------*/ 226 | 227 | mark { 228 | background-color: rgba(255, 255, 51, 0.5); 229 | border-bottom: 2px solid rgba(255, 153, 51, 0.3); 230 | padding: 1px; 231 | } 232 | 233 | /* vertical spacing after htmlwidgets */ 234 | .html-widget { 235 | margin-bottom: 10px; 236 | } 237 | -------------------------------------------------------------------------------- /docs/pkgdown.js: -------------------------------------------------------------------------------- 1 | /* http://gregfranko.com/blog/jquery-best-practices/ */ 2 | (function($) { 3 | $(function() { 4 | 5 | $("#sidebar") 6 | .stick_in_parent({offset_top: 40}) 7 | .on('sticky_kit:bottom', function(e) { 8 | $(this).parent().css('position', 'static'); 9 | }) 10 | .on('sticky_kit:unbottom', function(e) { 11 | $(this).parent().css('position', 'relative'); 12 | }); 13 | 14 | $('body').scrollspy({ 15 | target: '#sidebar', 16 | offset: 60 17 | }); 18 | 19 | $('[data-toggle="tooltip"]').tooltip(); 20 | 21 | var cur_path = paths(location.pathname); 22 | var links = $("#navbar ul li a"); 23 | var max_length = -1; 24 | var pos = -1; 25 | for (var i = 0; i < links.length; i++) { 26 | if (links[i].getAttribute("href") === "#") 27 | continue; 28 | // Ignore external links 29 | if (links[i].host !== location.host) 30 | continue; 31 | 32 | var nav_path = paths(links[i].pathname); 33 | 34 | var length = prefix_length(nav_path, cur_path); 35 | if (length > max_length) { 36 | max_length = length; 37 | pos = i; 38 | } 39 | } 40 | 41 | // Add class to parent
  • , and enclosing
  • if in dropdown 42 | if (pos >= 0) { 43 | var menu_anchor = $(links[pos]); 44 | menu_anchor.parent().addClass("active"); 45 | menu_anchor.closest("li.dropdown").addClass("active"); 46 | } 47 | }); 48 | 49 | function paths(pathname) { 50 | var pieces = pathname.split("/"); 51 | pieces.shift(); // always starts with / 52 | 53 | var end = pieces[pieces.length - 1]; 54 | if (end === "index.html" || end === "") 55 | pieces.pop(); 56 | return(pieces); 57 | } 58 | 59 | // Returns -1 if not found 60 | function prefix_length(needle, haystack) { 61 | if (needle.length > haystack.length) 62 | return(-1); 63 | 64 | // Special case for length-0 haystack, since for loop won't run 65 | if (haystack.length === 0) { 66 | return(needle.length === 0 ? 0 : -1); 67 | } 68 | 69 | for (var i = 0; i < haystack.length; i++) { 70 | if (needle[i] != haystack[i]) 71 | return(i); 72 | } 73 | 74 | return(haystack.length); 75 | } 76 | 77 | /* Clipboard --------------------------*/ 78 | 79 | function changeTooltipMessage(element, msg) { 80 | var tooltipOriginalTitle=element.getAttribute('data-original-title'); 81 | element.setAttribute('data-original-title', msg); 82 | $(element).tooltip('show'); 83 | element.setAttribute('data-original-title', tooltipOriginalTitle); 84 | } 85 | 86 | if(ClipboardJS.isSupported()) { 87 | $(document).ready(function() { 88 | var copyButton = ""; 89 | 90 | $(".examples, div.sourceCode").addClass("hasCopyButton"); 91 | 92 | // Insert copy buttons: 93 | $(copyButton).prependTo(".hasCopyButton"); 94 | 95 | // Initialize tooltips: 96 | $('.btn-copy-ex').tooltip({container: 'body'}); 97 | 98 | // Initialize clipboard: 99 | var clipboardBtnCopies = new ClipboardJS('[data-clipboard-copy]', { 100 | text: function(trigger) { 101 | return trigger.parentNode.textContent; 102 | } 103 | }); 104 | 105 | clipboardBtnCopies.on('success', function(e) { 106 | changeTooltipMessage(e.trigger, 'Copied!'); 107 | e.clearSelection(); 108 | }); 109 | 110 | clipboardBtnCopies.on('error', function() { 111 | changeTooltipMessage(e.trigger,'Press Ctrl+C or Command+C to copy'); 112 | }); 113 | }); 114 | } 115 | })(window.jQuery || window.$) 116 | -------------------------------------------------------------------------------- /docs/pkgdown.yml: -------------------------------------------------------------------------------- 1 | pandoc: 1.19.2.4 2 | pkgdown: 1.3.0 3 | pkgdown_sha: ~ 4 | articles: [] 5 | 6 | -------------------------------------------------------------------------------- /docs/reference/compose_data.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Compose response data from fixed and random effects — compose_data • funfact 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 48 | 49 | 50 | 51 | 52 | 53 |
    54 |
    55 | 90 | 91 | 92 |
    93 | 94 |
    95 |
    96 | 101 | 102 |
    103 | 104 |

    Compose response data from fixed and random effects

    105 | 106 |
    107 | 108 |
    compose_data(design_args, fixed = NULL, subj_rmx = NULL,
    109 |   item_rmx = NULL, verbose = FALSE, contr_type = "contr.dev")
    110 | 111 |

    Arguments

    112 | 113 | 114 | 115 | 116 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 |
    design_args

    List containing information about the experiment 117 | design; see stim_lists

    fixed

    vector of fixed effects

    subj_rmx

    matrix of by-subject random effects

    item_rmx

    matrix of by-item random effects

    verbose

    give debugging info (default = FALSE)

    contr_type

    contrast type (default "contr.dev"); see ?contrasts

    140 | 141 |

    Value

    142 | 143 |

    a data frame containing response variable Y, the 144 | linear sum of all fixed and random effects.

    145 | 146 |

    Details

    147 | 148 |

    This will add together all of the fixed and random effects 149 | according to the linear model specified by the design. Note, 150 | however, that it does not add in any residual noise; for that, use 151 | the function sim_norm.

    152 | 153 | 154 |
    155 | 166 |
    167 | 168 |
    169 | 172 | 173 |
    174 |

    Site built with pkgdown 1.3.0.

    175 |
    176 |
    177 |
    178 | 179 | 180 | 181 | 182 | 183 | 184 | -------------------------------------------------------------------------------- /docs/reference/contr.dev.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Deviation-Coded Contrast Matrices — contr.dev • funfact 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 48 | 49 | 50 | 51 | 52 | 53 |
    54 |
    55 | 90 | 91 | 92 |
    93 | 94 |
    95 |
    96 | 101 | 102 |
    103 | 104 |

    Return a matrix of deviation-coded contrasts.

    105 | 106 |
    107 | 108 |
    contr.dev(n, base = 1, contrasts = TRUE)
    109 | 110 |

    Arguments

    111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 121 | 122 | 123 | 124 | 125 | 126 |
    n

    a vector of levels for a factor, or the number of levels.

    base

    an integer specifying which group is considered the 120 | baseline group. Ignored if 'contrasts' is FALSE.

    contrasts

    a logical indicating whether contrasts should be computed.

    127 | 128 | 129 |
    130 | 137 |
    138 | 139 |
    140 | 143 | 144 |
    145 |

    Site built with pkgdown 1.3.0.

    146 |
    147 |
    148 |
    149 | 150 | 151 | 152 | 153 | 154 | 155 | -------------------------------------------------------------------------------- /docs/reference/design_formula.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Get the GLM formula for a factorially-designed experiment — design_formula • funfact 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 49 | 50 | 51 | 52 | 53 | 54 |
    55 |
    56 | 91 | 92 | 93 |
    94 | 95 |
    96 |
    97 | 102 | 103 |
    104 | 105 |

    Get the formula corresponding to the general linear model for a 106 | factorially designed experiment, with maximal random effects.

    107 | 108 |
    109 | 110 |
    design_formula(design_args, n_subj = NULL, dv_name = NULL,
    111 |   lme4_format = TRUE)
    112 | 113 |

    Arguments

    114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 |
    design_args

    A list with experimental design information (see link{stim_lists})

    n_subj

    Number of subjects

    dv_name

    Name of dependent variable; NULL (default) for one-sided formula

    lme4_format

    Do you want the results combined as the model formula for a lme4 model? (default TRUE)

    133 | 134 |

    Value

    135 | 136 |

    A formula, character string, or list, depending

    137 | 138 | 139 |
    140 | 149 |
    150 | 151 |
    152 | 155 | 156 |
    157 |

    Site built with pkgdown 1.3.0.

    158 |
    159 |
    160 |
    161 | 162 | 163 | 164 | 165 | 166 | 167 | -------------------------------------------------------------------------------- /docs/reference/fac_counts.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Calculate marginal and cell counts for factorially-designed data — fac_counts • funfact 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 48 | 49 | 50 | 51 | 52 | 53 |
    54 |
    55 | 90 | 91 | 92 |
    93 | 94 |
    95 |
    96 | 101 | 102 |
    103 | 104 |

    Calculate marginal and cell counts for factorially-designed data

    105 | 106 |
    107 | 108 |
    fac_counts(iv_names, dat, unit_names = c("subj_id", "item_id"))
    109 | 110 |

    Arguments

    111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 |
    iv_names

    Names of independent variables in data.frame given by dat.

    dat

    A data frame

    unit_names

    Names of the fields containing sampling units (subjects, items)

    126 | 127 |

    Value

    128 | 129 |

    a list, the elements of which have the marginal/cell counts for each factor in the design

    130 | 131 | 132 |
    133 | 142 |
    143 | 144 |
    145 | 148 | 149 |
    150 |

    Site built with pkgdown 1.3.0.

    151 |
    152 |
    153 |
    154 | 155 | 156 | 157 | 158 | 159 | 160 | -------------------------------------------------------------------------------- /docs/reference/gen_pop.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Generate population for data simulation — gen_pop • funfact 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 48 | 49 | 50 | 51 | 52 | 53 |
    54 |
    55 | 90 | 91 | 92 |
    93 | 94 |
    95 |
    96 | 101 | 102 |
    103 | 104 |

    Generate population for data simulation

    105 | 106 |
    107 | 108 |
    gen_pop(design_args, n_subj = NULL, fixed_ranges = NULL,
    109 |   var_range = c(0, 3), err_range = c(0, 6))
    110 | 111 |

    Arguments

    112 | 113 | 114 | 115 | 116 | 119 | 120 | 121 | 122 | 124 | 125 | 126 | 127 | 129 | 130 | 131 | 132 | 134 | 135 | 136 | 137 | 138 | 139 |
    design_args

    list specifying experimental design (see 117 | stim_lists); for best results, n_item should be 118 | 2*minimum number of items needed for the design

    n_subj

    number of subjects (for defining random effects 123 | structure; for best results, should be 2*number of stimulus lists

    fixed_ranges

    list of 2-element vectors (min, max) defining 128 | ranges of fixed effect parameters

    var_range

    2-element vector defining range (min, max) of 133 | random effect variances

    err_range

    2-element vector defining range of error variance

    140 | 141 |

    Value

    142 | 143 |

    list with parameters for data generation

    144 | 145 |

    See also

    146 | 147 | 148 | 149 | 150 |

    Examples

    151 |
    152 | dargs <- list(ivs = c(A = 2, B = 2), n_item = 8) 153 | gen_pop(dargs, 8)
    #> $fixed 154 | #> (Intercept) A2 B2 A2:B2 155 | #> 2.1401918 0.1956483 1.0626204 2.4755983 156 | #> 157 | #> $subj_rfx 158 | #> (Intercept) A2 B2 A2:B2 159 | #> (Intercept) 1.90939684 -1.2258088 0.09597889 0.1521171 160 | #> A2 -1.22580877 1.4370736 -0.16513874 0.4781659 161 | #> B2 0.09597889 -0.1651387 1.29651377 -1.3198060 162 | #> A2:B2 0.15211706 0.4781659 -1.31980598 2.1193015 163 | #> 164 | #> $item_rfx 165 | #> (Intercept) A2 B2 A2:B2 166 | #> (Intercept) 1.59472062 1.1896595 -0.01136419 0.8764575 167 | #> A2 1.18965948 1.4809110 0.98153066 0.5899103 168 | #> B2 -0.01136419 0.9815307 2.33792588 -0.2303857 169 | #> A2:B2 0.87645749 0.5899103 -0.23038566 0.6125350 170 | #> 171 | #> $err_var 172 | #> [1] 2.421229 173 | #>
    174 |
    175 | 188 |
    189 | 190 |
    191 | 194 | 195 |
    196 |

    Site built with pkgdown 1.3.0.

    197 |
    198 |
    199 |
    200 | 201 | 202 | 203 | 204 | 205 | 206 | -------------------------------------------------------------------------------- /docs/reference/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Function reference • funfact 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 45 | 46 | 47 | 48 | 49 | 50 |
    51 |
    52 | 87 | 88 | 89 |
    90 | 91 |
    92 |
    93 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 111 | 112 | 113 | 114 | 117 | 118 | 119 | 120 | 123 | 124 | 125 | 126 | 129 | 130 | 131 | 132 | 135 | 136 | 137 | 138 | 141 | 142 | 143 | 144 | 147 | 148 | 149 | 150 | 153 | 154 | 155 | 156 | 159 | 160 | 161 | 162 | 165 | 166 | 167 | 168 | 171 | 172 | 173 | 174 |
    108 |

    All functions

    109 |

    110 |
    115 |

    compose_data()

    116 |

    Compose response data from fixed and random effects

    121 |

    contr.dev()

    122 |

    Deviation-Coded Contrast Matrices

    127 |

    design_formula()

    128 |

    Get the GLM formula for a factorially-designed experiment

    133 |

    fac_counts()

    134 |

    Calculate marginal and cell counts for factorially-designed data

    139 |

    gen_pop()

    140 |

    Generate population for data simulation

    145 |

    sim_norm()

    146 |

    Sample data from population with normal error variance

    151 |

    stim_lists()

    152 |

    Generate stimulus presentation lists

    157 |

    term_names()

    158 |

    Get names for predictors in factorial design

    163 |

    trial_lists()

    164 |

    Generate trial lists from a stimulus presentation lists

    169 |

    with_dev_pred()

    170 |

    Generate numerical deviation-coded predictors

    175 |
    176 | 177 | 183 |
    184 | 185 |
    186 | 189 | 190 |
    191 |

    Site built with pkgdown 1.3.0.

    192 |
    193 |
    194 |
    195 | 196 | 197 | 198 | 199 | 200 | 201 | -------------------------------------------------------------------------------- /docs/reference/sim_norm.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Sample data from population with normal error variance — sim_norm • funfact 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 48 | 49 | 50 | 51 | 52 | 53 |
    54 |
    55 | 90 | 91 | 92 |
    93 | 94 |
    95 |
    96 | 101 | 102 |
    103 | 104 |

    Sample data from population with normal error variance

    105 | 106 |
    107 | 108 |
    sim_norm(design_args, n_subj, params, contr_type = "contr.dev",
    109 |   verbose = FALSE)
    110 | 111 |

    Arguments

    112 | 113 | 114 | 115 | 116 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 130 | 131 | 132 | 133 | 135 | 136 | 137 | 138 | 140 | 141 |
    design_args

    experiment design (see 117 | stim_lists); must contain sub-element n_item 118 | defining the number of items.

    n_subj

    number of subjects

    params

    list with parameters defining population 127 | (fixed, subj_rfx, item_rfx), and 128 | err_var; normally generated by a call to 129 | gen_pop.

    contr_type

    name of function defining IV contrast type; see 134 | ?contrasts

    verbose

    whether to return explicit information about 139 | individual random effects and residual error

    142 | 143 |

    Value

    144 | 145 |

    A data frame with simulated data

    146 | 147 |

    See also

    148 | 149 | 150 | 151 | 152 |

    Examples

    153 |
    design_args <- list(ivs = c(A = 2, B = 3), n_item = 18) 154 | pop_params <- gen_pop(design_args, 12) 155 | 156 | dat <- sim_norm(design_args, 12, pop_params)
    157 |
    158 | 171 |
    172 | 173 |
    174 | 177 | 178 |
    179 |

    Site built with pkgdown 1.3.0.

    180 |
    181 |
    182 |
    183 | 184 | 185 | 186 | 187 | 188 | 189 | -------------------------------------------------------------------------------- /docs/reference/term_names.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Get names for predictors in factorial design — term_names • funfact 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 50 | 51 | 52 | 53 | 54 | 55 |
    56 |
    57 | 92 | 93 | 94 |
    95 | 96 |
    97 |
    98 | 103 | 104 |
    105 | 106 |

    Get the names for the numerical predictors corresponding to all 107 | main effects and interactions of categorical IVs in a factorial 108 | design.

    109 | 110 |
    111 | 112 |
    term_names(design_args, design_formula = NULL,
    113 |   contr_type = "contr.dev")
    114 | 115 |

    Arguments

    116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 |
    design_args

    A list with experimental design information (see link{stim_lists})

    design_formula

    A formula (default NULL, constructs from design_args)

    contr_type

    Name of formula for generating contrasts (default "contr.dev")

    131 | 132 |

    Value

    133 | 134 |

    A character vector with names of all the terms

    135 | 136 | 137 |
    138 | 147 |
    148 | 149 |
    150 | 153 | 154 |
    155 |

    Site built with pkgdown 1.3.0.

    156 |
    157 |
    158 |
    159 | 160 | 161 | 162 | 163 | 164 | 165 | -------------------------------------------------------------------------------- /docs/reference/trial_lists.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Generate trial lists from a stimulus presentation lists — trial_lists • funfact 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 49 | 50 | 51 | 52 | 53 | 54 |
    55 |
    56 | 91 | 92 | 93 |
    94 | 95 |
    96 |
    97 | 102 | 103 |
    104 | 105 |

    Merge stimulus presentation lists with subject data to create a 106 | trial list.

    107 | 108 |
    109 | 110 |
    trial_lists(design_args, subjects = NULL, seq_assign = FALSE)
    111 | 112 |

    Arguments

    113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 126 | 127 | 128 | 129 | 131 | 132 |
    design_args

    Stimulus presentation lists (see stim_lists).

    subjects

    One of the following three: (1) an integer 122 | specifying the desired number of subjects (must be a multiple of 123 | number of stimulus lists); (2) a data frame with assignment 124 | information (must include a column subj_id); or (3) 125 | NULL, in which case there will be one subject per list.

    seq_assign

    If TRUE, assignment of subjects to lists will 130 | be sequential rather than random (default is FALSE)

    133 | 134 |

    Value

    135 | 136 |

    A data frame containing all trial information.

    137 | 138 | 139 |
    140 | 149 |
    150 | 151 |
    152 | 155 | 156 |
    157 |

    Site built with pkgdown 1.3.0.

    158 |
    159 |
    160 |
    161 | 162 | 163 | 164 | 165 | 166 | 167 | -------------------------------------------------------------------------------- /docs/reference/with_dev_pred.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Generate numerical deviation-coded predictors — with_dev_pred • funfact 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 48 | 49 | 50 | 51 | 52 | 53 |
    54 |
    55 | 90 | 91 | 92 |
    93 | 94 |
    95 |
    96 | 101 | 102 |
    103 | 104 |

    Add deviation-coded predictors to data frame.

    105 | 106 |
    107 | 108 |
    with_dev_pred(dat, iv_names = NULL)
    109 | 110 |

    Arguments

    111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 |
    dat

    A data frame with columns containing factors to be converted.

    iv_names

    Names of the variables to be converted.

    122 | 123 |

    Value

    124 | 125 |

    A data frame including additional deviation coded predictors.

    126 | 127 | 128 |

    Examples

    129 |
    with_dev_pred(stim_lists(list(ivs = c(A = 3))), "A")
    #> list_id item_id A AA2 AA3 130 | #> 1 1 1 A1 -0.3333333 -0.3333333 131 | #> 2 1 2 A2 0.6666667 -0.3333333 132 | #> 3 1 3 A3 -0.3333333 0.6666667 133 | #> 4 2 1 A2 0.6666667 -0.3333333 134 | #> 5 2 2 A3 -0.3333333 0.6666667 135 | #> 6 2 3 A1 -0.3333333 -0.3333333 136 | #> 7 3 1 A3 -0.3333333 0.6666667 137 | #> 8 3 2 A1 -0.3333333 -0.3333333 138 | #> 9 3 3 A2 0.6666667 -0.3333333
    139 |
    140 | 151 |
    152 | 153 | 162 |
    163 | 164 | 165 | 166 | 167 | 168 | 169 | -------------------------------------------------------------------------------- /man/compose_data.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stim_lists.R 3 | \name{compose_data} 4 | \alias{compose_data} 5 | \title{Compose response data from fixed and random effects} 6 | \usage{ 7 | compose_data(design_args, fixed = NULL, subj_rmx = NULL, 8 | item_rmx = NULL, verbose = FALSE, contr_type = "contr.dev") 9 | } 10 | \arguments{ 11 | \item{design_args}{List containing information about the experiment 12 | design; see \code{\link{stim_lists}}} 13 | 14 | \item{fixed}{vector of fixed effects} 15 | 16 | \item{subj_rmx}{matrix of by-subject random effects} 17 | 18 | \item{item_rmx}{matrix of by-item random effects} 19 | 20 | \item{verbose}{give debugging info (default = \code{FALSE})} 21 | 22 | \item{contr_type}{contrast type (default "contr.dev"); see ?contrasts} 23 | } 24 | \value{ 25 | a data frame containing response variable \code{Y}, the 26 | linear sum of all fixed and random effects. 27 | } 28 | \description{ 29 | Compose response data from fixed and random effects 30 | } 31 | \details{ 32 | This will add together all of the fixed and random effects 33 | according to the linear model specified by the design. Note, 34 | however, that it does not add in any residual noise; for that, use 35 | the function \code{\link{sim_norm}}. 36 | } 37 | -------------------------------------------------------------------------------- /man/contr.dev.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{contr.dev} 4 | \alias{contr.dev} 5 | \title{Deviation-Coded Contrast Matrices} 6 | \usage{ 7 | contr.dev(n, base = 1, contrasts = TRUE) 8 | } 9 | \arguments{ 10 | \item{n}{a vector of levels for a factor, or the number of levels.} 11 | 12 | \item{base}{an integer specifying which group is considered the 13 | baseline group. Ignored if 'contrasts' is \code{FALSE}.} 14 | 15 | \item{contrasts}{a logical indicating whether contrasts should be computed.} 16 | } 17 | \description{ 18 | Return a matrix of deviation-coded contrasts. 19 | } 20 | -------------------------------------------------------------------------------- /man/design_formula.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{design_formula} 4 | \alias{design_formula} 5 | \title{Get the GLM formula for a factorially-designed experiment} 6 | \usage{ 7 | design_formula(design_args, n_subj = NULL, dv_name = NULL, 8 | lme4_format = TRUE) 9 | } 10 | \arguments{ 11 | \item{design_args}{A list with experimental design information (see \code{link{stim_lists}})} 12 | 13 | \item{n_subj}{Number of subjects} 14 | 15 | \item{dv_name}{Name of dependent variable; \code{NULL} (default) for one-sided formula} 16 | 17 | \item{lme4_format}{Do you want the results combined as the model formula for a \code{lme4} model? (default \code{TRUE})} 18 | } 19 | \value{ 20 | A formula, character string, or list, depending 21 | } 22 | \description{ 23 | Get the formula corresponding to the general linear model for a 24 | factorially designed experiment, with maximal random effects. 25 | } 26 | -------------------------------------------------------------------------------- /man/fac_counts.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{fac_counts} 4 | \alias{fac_counts} 5 | \title{Calculate marginal and cell counts for factorially-designed data} 6 | \usage{ 7 | fac_counts(iv_names, dat, unit_names = c("subj_id", "item_id")) 8 | } 9 | \arguments{ 10 | \item{iv_names}{Names of independent variables in data.frame given by \code{dat}.} 11 | 12 | \item{dat}{A data frame} 13 | 14 | \item{unit_names}{Names of the fields containing sampling units (subjects, items)} 15 | } 16 | \value{ 17 | a list, the elements of which have the marginal/cell counts for each factor in the design 18 | } 19 | \description{ 20 | Calculate marginal and cell counts for factorially-designed data 21 | } 22 | -------------------------------------------------------------------------------- /man/gen_pop.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/sim_data.R 3 | \name{gen_pop} 4 | \alias{gen_pop} 5 | \title{Generate population for data simulation} 6 | \usage{ 7 | gen_pop(design_args, n_subj = NULL, fixed_ranges = NULL, 8 | var_range = c(0, 3), err_range = c(0, 6)) 9 | } 10 | \arguments{ 11 | \item{design_args}{list specifying experimental design (see 12 | \code{\link{stim_lists}}); for best results, n_item should be 13 | 2*minimum number of items needed for the design} 14 | 15 | \item{n_subj}{number of subjects (for defining random effects 16 | structure; for best results, should be 2*number of stimulus lists} 17 | 18 | \item{fixed_ranges}{list of 2-element vectors (min, max) defining 19 | ranges of fixed effect parameters} 20 | 21 | \item{var_range}{2-element vector defining range (min, max) of 22 | random effect variances} 23 | 24 | \item{err_range}{2-element vector defining range of error variance} 25 | } 26 | \value{ 27 | list with parameters for data generation 28 | } 29 | \description{ 30 | Generate population for data simulation 31 | } 32 | \examples{ 33 | 34 | dargs <- list(ivs = c(A = 2, B = 2), n_item = 8) 35 | gen_pop(dargs, 8) 36 | } 37 | \seealso{ 38 | \code{\link{sim_norm}} 39 | } 40 | -------------------------------------------------------------------------------- /man/sim_norm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/sim_data.R 3 | \name{sim_norm} 4 | \alias{sim_norm} 5 | \title{Sample data from population with normal error variance} 6 | \usage{ 7 | sim_norm(design_args, n_subj, params, contr_type = "contr.dev", 8 | verbose = FALSE) 9 | } 10 | \arguments{ 11 | \item{design_args}{experiment design (see 12 | \code{\link{stim_lists}}); must contain sub-element \code{n_item} 13 | defining the number of items.} 14 | 15 | \item{n_subj}{number of subjects} 16 | 17 | \item{params}{list with parameters defining population 18 | (\code{fixed}, \code{subj_rfx}, \code{item_rfx}), and 19 | \code{err_var}; normally generated by a call to 20 | \code{\link{gen_pop}}.} 21 | 22 | \item{contr_type}{name of function defining IV contrast type; see 23 | \code{?contrasts}} 24 | 25 | \item{verbose}{whether to return explicit information about 26 | individual random effects and residual error} 27 | } 28 | \value{ 29 | A data frame with simulated data 30 | } 31 | \description{ 32 | Sample data from population with normal error variance 33 | } 34 | \examples{ 35 | design_args <- list(ivs = c(A = 2, B = 3), n_item = 18) 36 | pop_params <- gen_pop(design_args, 12) 37 | 38 | dat <- sim_norm(design_args, 12, pop_params) 39 | } 40 | \seealso{ 41 | \code{\link{gen_pop}} 42 | } 43 | -------------------------------------------------------------------------------- /man/stim_lists.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stim_lists.R 3 | \name{stim_lists} 4 | \alias{stim_lists} 5 | \title{Generate stimulus presentation lists} 6 | \usage{ 7 | stim_lists(design_args, as_one = TRUE) 8 | } 9 | \arguments{ 10 | \item{design_args}{A list describing the experimental design, which 11 | must have an element \code{ivs}, giving a named list of independent 12 | variables, with each list element a vector giving the levels of 13 | that IV, or a single number stating the number of desired levels. 14 | If any IVs are administered between-subjects or between-items, 15 | these should be named in list elements \code{between_subj} and 16 | \code{between_item} respectively. The argument \code{design_args} 17 | also can optionally include the following two elements: 18 | \code{n_item}, the desired number of stimulus items, which if 19 | unspecified, will result in lists with the minimum possible number 20 | of items; and \code{n_rep}, the number of repetitions of each 21 | stimulus item for each participant (default 1).} 22 | 23 | \item{as_one}{boolean (default \code{TRUE}) specifying whether the 24 | presentation lists are to be returned as a single data frame or as 25 | elements in a list object} 26 | } 27 | \value{ 28 | a single \code{data.frame} (default) with each list 29 | identified by \code{list_id} or a \code{list} of dataframes, 30 | depending on the value of \code{as_one} 31 | } 32 | \description{ 33 | Generates counterbalanced presentation lists for factorially 34 | designed experiments involving stimulus presentation 35 | } 36 | \examples{ 37 | stim_lists(list(ivs = c(A = 2, B = 2))) # 2x2 within-subjects within-item 38 | 39 | stim_lists(list(ivs = c(A = 2, B = 2, n_item = 16))) # same but w/more items 40 | 41 | stim_lists(list(ivs = c(A = 2, B = 2, n_item = 16, n_rep = 3))) 42 | 43 | # mixed by subject, fully within by item 44 | stim_lists(list(ivs = list(group = c("adult", "child"), 45 | task = c("easy", "hard")), 46 | between_subj = "group", 47 | n_item = 12)) 48 | 49 | # mixed by subject, mixed by item 50 | stim_lists(list(ivs = c(A = 2, B = 2), 51 | between_subj = "A", 52 | between_item = "B")) 53 | } 54 | -------------------------------------------------------------------------------- /man/term_names.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{term_names} 4 | \alias{term_names} 5 | \title{Get names for predictors in factorial design} 6 | \usage{ 7 | term_names(design_args, design_formula = NULL, 8 | contr_type = "contr.dev") 9 | } 10 | \arguments{ 11 | \item{design_args}{A list with experimental design information (see \code{link{stim_lists}})} 12 | 13 | \item{design_formula}{A formula (default NULL, constructs from \code{design_args})} 14 | 15 | \item{contr_type}{Name of formula for generating contrasts (default "contr.dev")} 16 | } 17 | \value{ 18 | A character vector with names of all the terms 19 | } 20 | \description{ 21 | Get the names for the numerical predictors corresponding to all 22 | main effects and interactions of categorical IVs in a factorial 23 | design. 24 | } 25 | -------------------------------------------------------------------------------- /man/trial_lists.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stim_lists.R 3 | \name{trial_lists} 4 | \alias{trial_lists} 5 | \title{Generate trial lists from a stimulus presentation lists} 6 | \usage{ 7 | trial_lists(design_args, subjects = NULL, seq_assign = FALSE) 8 | } 9 | \arguments{ 10 | \item{design_args}{Stimulus presentation lists (see \code{\link{stim_lists}}).} 11 | 12 | \item{subjects}{One of the following three: (1) an integer 13 | specifying the desired number of subjects (must be a multiple of 14 | number of stimulus lists); (2) a data frame with assignment 15 | information (must include a column \code{subj_id}); or (3) 16 | \code{NULL}, in which case there will be one subject per list.} 17 | 18 | \item{seq_assign}{If TRUE, assignment of subjects to lists will 19 | be sequential rather than random (default is FALSE)} 20 | } 21 | \value{ 22 | A data frame containing all trial information. 23 | } 24 | \description{ 25 | Merge stimulus presentation lists with subject data to create a 26 | trial list. 27 | } 28 | -------------------------------------------------------------------------------- /man/with_dev_pred.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{with_dev_pred} 4 | \alias{with_dev_pred} 5 | \title{Generate numerical deviation-coded predictors} 6 | \usage{ 7 | with_dev_pred(dat, iv_names = NULL) 8 | } 9 | \arguments{ 10 | \item{dat}{A data frame with columns containing factors to be converted.} 11 | 12 | \item{iv_names}{Names of the variables to be converted.} 13 | } 14 | \value{ 15 | A data frame including additional deviation coded predictors. 16 | } 17 | \description{ 18 | Add deviation-coded predictors to data frame. 19 | } 20 | \examples{ 21 | with_dev_pred(stim_lists(list(ivs = c(A = 3))), "A") 22 | } 23 | --------------------------------------------------------------------------------