Skip to content

Commit ccc52f1

Browse files
Merge pull request #2 from AnthonyRaborn/devel
v0.4.2
2 parents d2ebd98 + a9a381a commit ccc52f1

31 files changed

+777
-435
lines changed

.Rbuildignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,4 @@ README.md
99
README.Rmd
1010
Tabu_example.R
1111
cran-comments.md
12-
README-Simulated Annealing example-1.png
13-
README-ACO example-1.png
14-
README-ACO example-2.png
15-
README-ACO example-3.png
12+
README-*

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
.Ruserdata
55
ShortForm.Rproj
66
iteration.html
7-
summary.txt
7+
summary.txt
8+

DESCRIPTION

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Package: ShortForm
22
Type: Package
33
Title: Automatic Short Form Creation
4-
Version: 0.4.1
5-
Date: 2018-06-12
6-
Authors@R: c(person("Walter", "Leite", email = "Walter.Leite@coe.ufl.edu", role = "aut"), person("Anthony", "Raborn", email = "anthony.w.raborn@gmail.com", role = c("aut", "cre")))
4+
Version: 0.4.2
5+
Date: 2018-10-16
6+
Authors@R: c(person("Anthony", "Raborn", email = "anthony.w.raborn@gmail.com", role = c("aut", "cre")), person("Walter", "Leite", email = "Walter.Leite@coe.ufl.edu", role = "aut"))
77
Description: Performs automatic creation of short forms of scales with an
88
ant colony optimization algorithm and a Tabu search. As implemented in the
99
package, the ant colony algorithm randomly selects items to build a model of
@@ -19,7 +19,7 @@ Description: Performs automatic creation of short forms of scales with an
1919
<doi:10.1080/10705511.2017.1409074> for an applied example of the Tabu search.
2020
License: LGPL (>= 2.0, < 3) | Mozilla Public License
2121
LazyData: TRUE
22-
RoxygenNote: 6.0.1
22+
RoxygenNote: 6.1.0
2323
Suggests:
2424
knitr,
2525
MplusAutomation (>= 0.7),
@@ -34,3 +34,5 @@ Depends:
3434
Roxygen: list(wrap = FALSE)
3535
URL: https://github.com/AnthonyRaborn/ShortForm
3636
BugReports: https://github.com/AnthonyRaborn/ShortForm/issues
37+
Encoding: UTF-8
38+

NAMESPACE

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(plot,antcolony)
4+
S3method(plot,simulatedAnnealing)
5+
S3method(plot,tabu)
36
export(add.param)
47
export(antcolony.lavaan)
58
export(antcolony.mplus)
6-
export(antcolony_plot)
79
export(refit.model)
810
export(search.prep)
911
export(simulatedAnnealing)
1012
export(tabu.sem)
1113
export(tabuShortForm)
1214
import(lavaan)
1315
import(utils)
16+
importFrom(stats,runif)

NEWS.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
# Package v0.4.2
2-
## Goals:
2+
## Introduction of Function Classes
3+
* This version introduces classes to each of the main function outputs (`antcolony.lavaan` == "antcolony", `simulatedAnnealing` == "simulatedAnnealing", `tabuShortForm` == "tabu")
4+
* This will allow for `plot` and `print` methods for each of these functions
35

4-
* Create an option for an initial short form to feed into the functions as a starting point
6+
## Broken functionality: antcolony_plot
7+
* The function `antcolony_plot` was replaced with `plot.antcolony` (aka, a plot method)
58

6-
## Bugfixes:
9+
## New functionality: S3 method for plotting
10+
* With the addition of classes, a plot method and accompanying documentation has been added.
11+
* Future updates will expand S3 methods for `print()` and `summary()`
712

8-
* Fixed a situation where simulatedAnnealing returns the initial model
9-
* Fixed simulatedAnnealing not correctly accepting a user-defined restartCriteria
13+
## Bugfixes
14+
* Bugfixes to the main functions when using them to create shortforms of bifactor models.
15+
* They should now produce actual bifactor shortforms (in v0.4.1, sometimes the item names would be cut off in later iterations [FIXED] and the relationship between latent variables would be changed when they should have been kept constant [FIXED]).
16+
17+
## Added a package loading message
18+
* Now, when using an interactive R session, a package message is printed out (with a little Penguin holding it all up)!
1019

1120
# Package v0.4.1
1221
## New functionality: Simulated Annealing

R/ACO_lavaan.R

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ antcolony.lavaan = function(data = NULL, sample.cov = NULL, sample.nobs = NULL,
235235

236236
#creates a vector with all items. UNIQUE USED FOR CASES WHEN ITEMS CROSS-LOAD
237237
item.vector = unique(unlist(list.items, use.names = F))
238+
if (!is.null(bifactor)) {
239+
item.vector = item.vector[which(item.vector!=bifactor)]
240+
}
238241

239242
#reads the Lavaan model syntax input into the function
240243
input = unlist(strsplit(antModel, '\n'))
@@ -358,8 +361,13 @@ antcolony.lavaan = function(data = NULL, sample.cov = NULL, sample.nobs = NULL,
358361
# Check the above messages and set pheromone to zero under certain circumstances
359362
# the circumstances in question:
360363
bad.warnings <- c("WARNING: could not compute standard errors",
361-
"WARNING: could not compute scaled test statistic", "WARNING: covariance matrix of latent variables is not positive definite", "WARNING: model has NOT converged")
362-
bad.errors <- c("ERROR: initial model-implied matrix (Sigma) is not positive definite","ERROR: initial model-implied matrix (Sigma) is not positive definite", "ERROR: missing observed variables in dataset")
364+
"WARNING: could not compute scaled test statistic",
365+
"WARNING: covariance matrix of latent variables is not positive definite",
366+
"WARNING: model has NOT converged",
367+
"WARNING: could not invert information matrix",
368+
"WARNING: the optimizer warns that a solution has NOT been found")
369+
bad.errors <- c("ERROR: initial model-implied matrix (Sigma) is not positive definite",
370+
"ERROR: missing observed variables in dataset")
363371
if(any(errors %in% bad.errors) || any(warnings %in% bad.warnings)){
364372
pheromone = 0
365373
if(verbose == TRUE) {
@@ -529,8 +537,10 @@ antcolony.lavaan = function(data = NULL, sample.cov = NULL, sample.nobs = NULL,
529537

530538
final.solution = matrix(c(best.so.far.fit.indices,best.so.far.pheromone,best.so.far.solution),1,,
531539
dimnames=list(NULL,c(names(model.fit),"mean_gamma",item.vector)))
532-
540+
results = list(final.solution, summary, 'best.model' = best.so.far.model, 'best.syntax' = best.so.far.syntax)
541+
class(results) = "antcolony"
533542
#FINISH FUNCTION.
534543
# return(list(final.solution, results, summary))
535-
return(list(final.solution, summary, 'best.model' = best.so.far.model, 'best.syntax' = best.so.far.syntax))
544+
545+
return(results)
536546
}

R/ShortForm.R

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#' \code{ShortForm} package
2+
#'
3+
#' Automated Item Selection Algorithms for Short Forms
4+
#'
5+
#' See the README on \href{https://github.com/AnthonyRaborn/ShortForm#readme}{GitHub} for more information.
6+
#'
7+
#' @docType package
8+
#' @name ShortForm
9+
#' @importFrom stats runif
10+
NULL
11+
12+
## quiets concerns of R CMD check about internal variables
13+
if(getRversion() >= "2.15.1") {
14+
utils::globalVariables(c("allItems", "auto.cov.lv.x", "auto.cov.y", "auto.delta", "auto.fix.first",
15+
"auto.fix.single", "auto.th", "auto.var", "bestModel", "currentModel", "estimator",
16+
"factors", "int.lv.free", "int.ov.free", "itemsPerFactor", "model.type", "numItems",
17+
"std.lv"
18+
))
19+
}
20+
21+
#' Create Package Startup Message
22+
#'
23+
#' Makes package startup message.
24+
#'
25+
#' Idea taken from https://github.com/ntguardian/MCHT/blob/master/R/StartupMessage.R
26+
#'
27+
#' @import utils
28+
#' @examples
29+
#' ShortForm:::ShortFormStartup()
30+
31+
ShortFormStartup <- function() {
32+
ShortForm <- c(" ##### ####### \n # # # # #### ##### ##### # #### ##### # # \n # # # # # # # # # # # # # ## ## \n ##### ###### # # # # # ##### # # # # # ## # \n # # # # # ##### # # # # ##### # # \n # # # # # # # # # # # # # # # # \n ##### # # #### # # # # #### # # # # \n ")
33+
version <- paste("\t\t Version", as.character(utils::packageVersion("ShortForm")))
34+
penguin <- c("\t\t\t (o<", "\t\t\t //\\", "\t\t\t V_/_ ")
35+
36+
message <- c(ShortForm, version, penguin)
37+
38+
cat(message, sep = "\n")
39+
}
40+
41+
#' Package Attach Hook Function
42+
#'
43+
#' Hook triggered when package attached.
44+
#'
45+
#' Idea taken from https://github.com/ntguardian/MCHT/blob/master/R/StartupMessage.R
46+
#'
47+
#' @param lib a character string giving the library directory where the package
48+
#' defining the namespace was found
49+
#' @param pkg a character string giving the name of the package
50+
#' @examples
51+
#' ShortForm:::.onAttach(.libPaths()[1], "ShortForm")
52+
53+
.onAttach <- function(lib, pkg) {
54+
msg <- ShortFormStartup()
55+
if (!interactive())
56+
msg[1] <- paste("Package 'ShortForm' version", packageVersion("ShortForm"))
57+
packageStartupMessage(msg)
58+
invisible()
59+
}

R/Tabu_sem.R

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ tabu.sem <- function(init.model,
4747
tabu.size = 5) {
4848
# source(Tabu_internal.R)
4949
# Initialize objective function and best model
50-
best.obj <- current.obj <- obj(init.model)
51-
best.model <- current.model <- init.model
52-
best.binvec <- current.binvec <- ptab
50+
best.obj<-all.obj<-current.obj<-obj(init.model)
51+
best.model<-current.model<-init.model
52+
best.binvec<-current.binvec<-ptab
5353

5454
tabu.list <- vector("numeric")
5555

5656
# Do iterations
57-
for (it in 1:niter) {
58-
print(paste0("Running iteration ", it, "."))
57+
for(it in 1:niter){
58+
cat(paste0("\rRunning iteration ", it, " of ", niter, ". "))
5959
# Loop through all neighbors
6060
tmp.obj <- vector("numeric")
6161
tmp.mod <- list()
@@ -87,9 +87,10 @@ tabu.sem <- function(init.model,
8787
indx <- which.min(tmp.obj[valid])
8888

8989
# Move current state to next model
90-
current.obj <- (tmp.obj[valid])[indx]
91-
current.mod <- (tmp.mod[valid])[[indx]]
92-
current.binvec <- (tmp.vec[valid])[[indx]]
90+
current.obj<-(tmp.obj[valid])[indx]
91+
all.obj<-c(all.obj,current.obj)
92+
current.mod<-(tmp.mod[valid])[[indx]]
93+
current.binvec<-(tmp.vec[valid])[[indx]]
9394

9495
# Update Tabu list
9596
tabu.list <- c(valid[indx], tabu.list)
@@ -106,10 +107,12 @@ tabu.sem <- function(init.model,
106107
}
107108
}
108109

109-
ret <- list()
110-
ret$best.obj <- best.obj
111-
ret$best.mod <- best.mod
112-
ret$best.binvec <- best.binvec
110+
ret<-list()
111+
ret$best.obj<-best.obj
112+
ret$best.mod<-best.mod
113+
ret$best.binvec<-best.binvec
114+
ret$all.obj<-all.obj
115+
class(ret)<-"tabu"
113116

114117
return(ret)
115118
}

R/Tabu_shortform.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ tabuShortForm <-
134134
initialShortModel <- randomInitialModel()
135135
cat("The initial short form is: \n")
136136
cat(paste(initialShortModel$model.syntax, collapse = "\n"))
137-
best.obj <-
138-
current.obj <- criterion(initialShortModel$lavaan.output)
137+
best.obj <- all.obj <- current.obj <- criterion(initialShortModel$lavaan.output)
139138
best.model <- current.model <- initialShortModel$lavaan.output
140139
tabu.list <- vector("numeric")
141140

@@ -350,6 +349,7 @@ tabuShortForm <-
350349

351350
# Move current state to next model
352351
current.obj <- (tmp.obj[valid])[indx]
352+
all.obj <- c(all.obj, current.obj)
353353
current.mod <- (tmp.mod[valid])[indx]
354354
current.syntax <- unlist((tmp.syntax[valid])[indx])
355355
# current.binvec<-(tmp.vec[valid])[[indx]]
@@ -373,8 +373,9 @@ tabuShortForm <-
373373

374374
ret <- list()
375375
ret$best.obj <- best.obj
376-
ret$best.mod <- best.mod
376+
ret$best.mod <- best.mod[[1]]
377377
ret$best.syntax <- best.syntax
378-
378+
ret$all.obj <- all.obj
379+
class(ret) = "tabu"
379380
return(ret)
380381
}

0 commit comments

Comments
 (0)