File: lavaan.R

package info (click to toggle)
r-cran-semplot 1.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 464 kB
  • sloc: makefile: 2
file content (117 lines) | stat: -rw-r--r-- 3,707 bytes parent folder | download
1
2
3
4
5
6
7
8
9
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
113
114
115
116
117
### Path diagrams ###
# 
# setMethod("semPaths.S4",signature("lavaan"),function(object,...){
#   invisible(semPaths(semPlotModel(object),...))
# })
# 


## EXTRACT MODEL ###
setMethod("semPlotModel_S4",signature("lavaan"),function(object){

  if (is(object,"blavaan")) class(object) <- 'lavaan'
  if (!is(object,"lavaan")) stop("Input must me a 'lavaan' object")

  
  # Extract parameter estimates:
  pars <- parameterEstimates(object,standardized=TRUE)
  list <- inspect(object,"list")
  
  # Remove mean structure (TEMP SOLUTION)
  # meanstructure <- pars$op=="~1"
  # pars <- pars[!meanstructure,]
  
  # Extract variable and factor names:
  # varNames <- fit@Model@dimNames$lambda[[1]]
  # factNames <- fit@Model@dimNames$lambda[[2]]
#   Lambda <- inspect(object,"coef")$lambda
  varNames <- lavaanNames(object, type="ov")
  factNames <- lavaanNames(object, type="lv")
#   rm(Lambda)
  
  factNames <- factNames[!factNames%in%varNames]
  
  # Extract number of variables and factors
  n <- length(varNames)
  k <- length(factNames)
  
  # Extract parameter names:
  if (is.null(pars$label)) pars$label <- rep("",nrow(pars))
  
  semModel <- new("semPlotModel")

  if (is.null(pars$group)) pars$group <- ""

  # Create edges dataframe
  semModel@Pars <- data.frame(
    label = pars$label,
    lhs = ifelse(pars$op=="~"|pars$op=="~1",pars$rhs,pars$lhs),
    edge = "--",
    rhs = ifelse(pars$op=="~"|pars$op=="~1",pars$lhs,pars$rhs),
    est = pars$est,
    std = pars$std.all,
    group = pars$group,
    fixed = list$free[list$op!="=="]==0,
    par = list$free[list$op!="=="],
    stringsAsFactors=FALSE)


  semModel@Pars$edge[pars$op=="~~"] <- "<->"  
  semModel@Pars$edge[pars$op=="~*~"] <- "<->"  
  semModel@Pars$edge[pars$op=="~"] <- "~>"
  semModel@Pars$edge[pars$op=="=~"] <- "->"
  semModel@Pars$edge[pars$op=="~1"] <- "int"
  semModel@Pars$edge[grepl("\\|",pars$op)] <- "|"
  
  # Move thresholds to Thresholds slot:
  semModel@Thresholds <- semModel@Pars[grepl("\\|",semModel@Pars$edge),-(3:4)]
  
  # Remove constraints and weird stuff:
  semModel@Pars  <- semModel@Pars[!pars$op %in% c('<', '>',':=','<','>','==','|'),]
  
  # Remove thresholds from Pars:
#   semModel@Pars <- semModel@Pars[!grepl("\\|",semModel@Pars$edge),]
  
  semModel@Vars <- data.frame(
    name = c(varNames,factNames),
    manifest = c(varNames,factNames)%in%varNames,
    exogenous = NA,
    stringsAsFactors=FALSE)
    
  # res.cov <- lavTech(object, "sampstat")$res.cov
  # lavTech(object, "sampstat")$cov
  # if (!is.null(res.cov) && !length(res.cov) == 0){
      # if (!is.null(res.cov[[1]])){
      #   semModel@ObsCovs <- object@SampleStats@res.cov    
      # } else {
      #   semModel@ObsCovs <- object@SampleStats@cov
      # }    
  # } else {
  #   semModel@ObsCovs <- list(matrix(NA,
  #          length(varNames),length(varNames)))
  # } 
  
  # Use add.labels=TRUE so lavTech returns named matrices (handles multigroup with different vars)
  if (lavInspect(object, "options")$conditional.x){
    semModel@ObsCovs <- lapply(lavTech(object, "sampstat", add.labels = TRUE),"[[","res.cov")
  } else {
    semModel@ObsCovs <- lapply(lavTech(object, "sampstat", add.labels = TRUE),"[[","cov")
  }
  names(semModel@ObsCovs) <- lavInspect(object, "group.label")

  if (lavInspect(object, "options")$conditional.x){
    semModel@ImpCovs <- lapply(lavTech(object, "implied", add.labels = TRUE), "[[", "res.cov")
  } else {
    semModel@ImpCovs <- lapply(lavTech(object, "implied", add.labels = TRUE), "[[", "cov")
  }
  names(semModel@ImpCovs) <- lavInspect(object, "group.label") # object@Data@group.label
  
  semModel@Computed <- TRUE
  
  semModel@Original <- list(object)
  
  return(semModel)
})