File: amos.R

package info (click to toggle)
r-cran-semplot 1.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 464 kB
  • sloc: makefile: 2
file content (215 lines) | stat: -rw-r--r-- 6,685 bytes parent folder | download | duplicates (4)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

semPlotModel_Amos <- function(object)
{
  ## Warnings:
  warning("(Residual) variances of Amos model is not yet supported")
  
  # Read characters:
  str <- readChar(object,nchars=file.info(object)$size)
  
  # Extract Estimates section:
  estLocs <- gregexpr('<div ntype="estimates" nodecaption="Estimates" caption="Estimates">',str)[[1]]
  nModel <- length(estLocs)
  Parss <- list()
  
  # Open and close div:
  open <- gregexpr("<div",str)[[1]]
  close <- gregexpr("</div>",str)[[1]]
  
  for (mod in 1:nModel)
  {  
    startSect <- which(open==estLocs[mod])
    # Find title:
    titleString <- substring(str,open[startSect+1],close[which(close>open[startSect+1])[1]])
    modName <- regmatches(titleString,regexpr("(?<=<h5>).*?(?=</h5>)",titleString,perl=TRUE))
    
    # Find close:
    nest <- 1
    curOpen <- startSect + 1
    curClose <- which(close>open[curOpen])[1]
    repeat{
      # If next is opened:
      if (open[curOpen] < close[curClose])
      {
        nest <- nest + 1
        curOpen <- curOpen + 1
      } else {
        # If next is closed:
        nest <- nest - 1
        if (nest==0) break
        curClose <- curClose + 1
      }
    }
    EstTabs <- substring(str,open[startSect],close[curClose] + 5)
    
    # Extract tables:
    Tabs <- readHTMLTable(EstTabs)
    
    # Find names of tables;
    Tabspl <- strsplit(EstTabs,split="<table>")[[1]]
    Names <- regmatches(Tabspl,gregexpr('(?<=nodecaption=").*(?=">)',Tabspl,perl=TRUE))[-length(Tabspl)]
    Names <- sapply(Names,function(x)x[length(x)])
    
    names(Tabs) <- Names
    
    # Regression weights:
    Reg <- Tabs[[which(grepl("regression",names(Tabs),ignore.case=TRUE))[1]]]
    Reg <-  as.data.frame(lapply(Reg,as.character),stringsAsFactors=FALSE)
    if (is.null(Reg$Estimate)) Reg$Estimate <- 1
    if (is.null(Reg$Label)) Reg$Label <- ""
    
    # Make Pars:
    # Define Pars:
    Pars <- data.frame(
      label = Reg$Label, 
      lhs = Reg[,3],
      edge = "->",
      rhs = Reg[,1],
      est = as.numeric(gsub(",",".",Reg$Estimate)),
      std = NA,
      group = modName,
      fixed = FALSE,
      par = 0,
      stringsAsFactors=FALSE)
    
#     Pars$par <- 1:nrow(Pars)
    Pars$label[is.na(Pars$label)] <- ""
    
#     # Fix edges:
#     Pars$edge[Reg[,2]=="<---"] <- "->"
#     Pars$edge[Reg[,2]=="<-->"] <- "<->"
    
    # Test for fixed:
    if (!is.null(Reg$P)) Pars$fixed <- is.na(Reg$P)
    
    # Standardized values:
    if (any(grepl("standardized",names(Tabs),ignore.case=TRUE)))
    {
      Std <- Tabs[[which(grepl("standardized",names(Tabs),ignore.case=TRUE))[1]]]
      Std <- as.data.frame(lapply(Std,as.character),stringsAsFactors=FALSE)
      if (is.null(Std$Estimate)) Std$Estimate <- 1
      Pars$std <- as.numeric(gsub(",",".",Std$Estimate))
    }
    
    # Add covariances:
    if (any(grepl("covariance",names(Tabs),ignore.case=TRUE)))
    {
      Cov <- Tabs[[which(grepl("covariance",names(Tabs),ignore.case=TRUE))[1]]]
      Cov <- as.data.frame(lapply(Cov,as.character),stringsAsFactors=FALSE)
      if (is.null(Cov$Estimate)) Cov$Estimate <- 1
      if (is.null(Cov$Label)) Cov$Label <- ""
      
      covPars <- data.frame(
        label = Cov$Label, 
        lhs = Cov[,3],
        edge = "<->",
        rhs = Cov[,1],
        est = as.numeric(gsub(",",".",Cov$Estimate)),
        std = NA,
        group = modName,
        fixed = FALSE,
        par = 0,
        stringsAsFactors=FALSE)
      
        if (!is.null(Cov$P)) covPars$fixed <- is.na(Cov$P)
      
        # Check cors:
      if (any(grepl("correlation",names(Tabs),ignore.case=TRUE)))
      {
        Cor <- Tabs[[which(grepl("correlation",names(Tabs),ignore.case=TRUE))[1]]]
        Cor <- as.data.frame(lapply(Cor,as.character),stringsAsFactors=FALSE)
        if (is.null(Cor$Estimate)) Cor$Estimate <- 1
        covPars$std <- Cor$Estimate
      }
      Pars <- rbind(Pars,covPars)
    }    
    
    Parss[[mod]] <- Pars
  }
  
  Pars <- do.call(rbind,Parss)
  Pars$par <- 1:nrow(Pars)
  
  ## Extract variable info:
  startSect <- which(open==gregexpr('<div nodecaption="Variable list"',str)[[1]])
  nest <- 1
  curOpen <- startSect + 1
  curClose <- which(close>open[curOpen])[1]
  repeat{
    # If next is opened:
    if (open[curOpen] < close[curClose])
    {
      nest <- nest + 1
      curOpen <- curOpen + 1
    } else {
      nest <- nest - 1
      if (nest==0) break
      curClose <- curClose + 1
    }
  }
  VarList <- substring(str,open[startSect],close[curClose] + 5)
  # Reove html tags:
  VarList <- gsub("<(.|\n)*?>","",VarList)
  # Per line:
  VarList <-  scan(text=VarList,what="character",sep="\n")
  # Remove leading and trailing whitespace:
  VarList <- gsub("^\\s*","",VarList)
  VarList <- gsub("\\s*$","",VarList)
  
  AllVars <- unique(c(Pars$lhs,Pars$rhs))
  AllVars <- AllVars[AllVars!=""]

  # Location of indicators:
  # manEndo - latEndo - manExo - latExo
  grepRep <- function(...)
  {
    res <- grep(...)
    if (length(res)==0) res <- -1
    return(res[1])
  }
  ind <- c(
    grepRep("Observed, endogenous variables",VarList),
    grepRep("Unobserved, endogenous variables",VarList),
    grepRep("Observed, exogenous variables",VarList),
    grepRep("Unobserved, exogenous variables",VarList))
  
  if (ind[1] > 0)
  {
    manEndo <- VarList[(ind[1]+1):(which((1:length(VarList) == length(VarList)) | (1:length(VarList) > ind[1]+1 & 1:length(VarList) %in% ind))[1] - 1)]
  } else manEndo <- character(0)
  
  if (ind[2] > 0)
  {
    latEndo <- VarList[(ind[2]+1):(which((1:length(VarList) == length(VarList)) | (1:length(VarList) > ind[2]+1 & 1:length(VarList) %in% ind))[1] - 1)]
  } else latEndo <- character(0)
  
  if (ind[3] > 0)
  {
    manExo <- VarList[(ind[3]+1):(which((1:length(VarList) == length(VarList)) | (1:length(VarList) > ind[3]+1 & 1:length(VarList) %in% ind))[1] - 1)]
  } else manExo <- character(0)
  
  if (ind[4] > 0)
  {
    latExo <- VarList[(ind[4]+1):(which((1:length(VarList) == length(VarList)) | (1:length(VarList) > ind[4]+1 & 1:length(VarList) %in% ind))[1] - 1)]
  } else latExo <- character(0)
  
  Vars <- data.frame(
    name = c(manEndo,manExo,latEndo,latExo),
    manifest = c(rep(TRUE,length(c(manEndo,manExo))),rep(FALSE,length(c(latEndo,latExo)))),
    exogenous = c(rep(FALSE,length(manEndo)),rep(TRUE,length(manExo)),rep(FALSE,length(latEndo)),rep(TRUE,length(latExo))),
    stringsAsFactors=FALSE)
  
  
  
  # Return:
  semModel <- new("semPlotModel")
  semModel@Pars <- Pars
  semModel@Vars <- Vars
  semModel@Computed <- FALSE
  semModel@Original <- list(str)
  semModel@ObsCovs <- list()
  semModel@ImpCovs <- list()
  # semModel@Thresholds <- Thresh
  
  return(semModel)
}