Class: Statsample::SEM::OpenMxEngine

Inherits:
Object
  • Object
show all
Includes:
DirtyMemoize, Summarizable
Defined in:
lib/statsample/sem/openmxengine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, opts = Hash.new) ⇒ OpenMxEngine

Returns a new instance of OpenMxEngine.



11
12
13
14
15
16
17
18
# File 'lib/statsample/sem/openmxengine.rb', line 11

def initialize(model,opts=Hash.new)
  @model=model
  defaults = {
    :name=>_("SEM analysis using OpenMx")
  }
  @opts=defaults.merge defaults
  @name=@opts[:name]
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/statsample/sem/openmxengine.rb', line 9

def name
  @name
end

#r_summaryObject (readonly)

Returns the value of attribute r_summary.



10
11
12
# File 'lib/statsample/sem/openmxengine.rb', line 10

def r_summary
  @r_summary
end

#summarizableObject

Returns the value of attribute summarizable.



8
9
10
# File 'lib/statsample/sem/openmxengine.rb', line 8

def summarizable
  @summarizable
end

Instance Method Details

#bicObject



121
122
123
124
125
126
# File 'lib/statsample/sem/openmxengine.rb', line 121

def bic
  k=@model.k
  p k
  ln_n=Math.log(@model.cases)
  chi_square+((k*(k-1).quo(2)) - df)*ln_n
end

#cfiObject



117
118
119
120
# File 'lib/statsample/sem/openmxengine.rb', line 117

def cfi
  d_null=chi_square_null-df_null
  ((d_null)-(chi_square-df)).quo(d_null)
end

#chi_squareObject



84
85
86
# File 'lib/statsample/sem/openmxengine.rb', line 84

def chi_square
  @r_summary['Chi']
end

#chi_square_nullObject



90
91
92
# File 'lib/statsample/sem/openmxengine.rb', line 90

def chi_square_null
  null_model.r_summary['Chi']
end

#coefficientsObject



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/statsample/sem/openmxengine.rb', line 127

def coefficients
  est=Hash.new
  coeffs=@r_summary['parameters']
  # 0:name, 1:matrix, 2:row, 3:col, 4:estimate, 5:Std.error
  coeffs[0].each_with_index do |v,i|
    f1=coeffs[2][i]
    f2=coeffs[3][i]
    key=[f1,f2].sort
    est[key]={:estimate=>coeffs[4][i], :se=>coeffs[5][i], :z=>nil, :p=>nil, :label=>v}
  end
  est
  
end

#computeObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/statsample/sem/openmxengine.rb', line 61

def compute
  raise "Insuficient information" unless @model.complete?
  r.assign 'data', @model.data_type==:raw ? @model.ds : @model.matrix
  if @model.matrix
    r.assign 'vn', @model.variables
    # We should assing names to fields on matrix
    r.void_eval('dimnames(data)<-list(vn,vn)')
  end
  r.assign 'manifests',@model.manifests
  r.assign 'latents', @model.latents
  r.assign 'd_means',@model.means unless @model.means.nil?
  r.void_eval r_query
  @r_summary=@r.eval('summary(factorFit)').to_ruby
  true
end

#compute_null_modelObject

:nodoc:



100
101
102
103
104
# File 'lib/statsample/sem/openmxengine.rb', line 100

def compute_null_model #:nodoc:
  nm=@model.dup
  nm.make_null
  (self.class).new(nm,@opts)
end

#dfObject



87
88
89
# File 'lib/statsample/sem/openmxengine.rb', line 87

def df
  @r_summary['degreesOfFreedom']
end

#df_nullObject



93
94
95
# File 'lib/statsample/sem/openmxengine.rb', line 93

def df_null
  null_model.r_summary['degreesOfFreedom']
end

#graphvizObject



76
77
78
79
80
81
82
83
# File 'lib/statsample/sem/openmxengine.rb', line 76

def graphviz
  compute if @r_summary.nil?
  tf=Tempfile.new('model.dot')
  r.void_eval("omxGraphviz(factorModel,'#{tf.path}')")
#        tf.close
#        tf.open
  tf.read
end

#nfiObject

χ2(Null Model) - χ2(Proposed Model)]/ [χ2(Null Model)


109
110
111
# File 'lib/statsample/sem/openmxengine.rb', line 109

def nfi
  (chi_square_null-chi_square).quo(chi_square_null)
end

#nnfiObject

χ2/df(Null Model) - χ2/df(Proposed Model)]/[χ2/df(Null Model) - 1


114
115
116
# File 'lib/statsample/sem/openmxengine.rb', line 114

def nnfi
  (chi_square_null.quo(df_null) - chi_square.quo(df)).quo(chi_square_null.quo(df_null)-1)
end

#null_modelObject



97
98
99
# File 'lib/statsample/sem/openmxengine.rb', line 97

def null_model
  @null_model||=compute_null_model
end

#rObject



19
20
21
# File 'lib/statsample/sem/openmxengine.rb', line 19

def r
  @r||=Rserve::Connection.new
end

#r_mxdataObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/statsample/sem/openmxengine.rb', line 29

def r_mxdata
  type=case @model.data_type
    when :raw
      'raw'
    when :covariance
      'cov'
    when :correlation
      'cor'
  end
  means=(@model.data_type!=:raw and !@model.means.nil?) ? ", means = d_means " : ""
  num=(@model.data_type!=:raw) ? ", numObs = #{@model.cases} " : ""
  
  "mxData(observed=data, type='#{type}' #{means} #{num})"
end

#r_mxpathsObject



22
23
24
25
26
27
28
# File 'lib/statsample/sem/openmxengine.rb', line 22

def r_mxpaths
  @model.paths.values.map {|path|
    value= path[:value] ? ", values = #{path[:value]}":""
    label= path[:label] ? ", labels = \"#{path[:label]}\"" : ""  
    "mxPath(from=c(\"#{path[:from]}\"), to=c(\"#{path[:to]}\"), arrows = #{path[:arrow]}, free = #{path[:free] ? "TRUE" : "FALSE"} #{value} #{label})"
  }.join(",\n")
end

#r_queryObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/statsample/sem/openmxengine.rb', line 44

def r_query
  <<-EOF
library(OpenMx);
factorModel <- mxModel(
name="#{name}",
type="RAM",
manifestVars = manifests,
latentVars = latents,
#{r_mxpaths}, 
#{r_mxdata}
);
factorFit<-mxRun(factorModel);
rm(data,manifests,latents,d_means);
  EOF
  #p r.eval('factorFit').to_ruby
end

#rmseaObject



105
106
107
# File 'lib/statsample/sem/openmxengine.rb', line 105

def rmsea
  @r_summary['RMSEA']
end