gragusa / InstrumentalVariables.jl

A Julia package for Instrumental Variables models
Other
3 stars 1 forks source link

how to access elements of coeftable #1

Open remlapmot opened 8 years ago

remlapmot commented 8 years ago

Hi, Thanks indeed for this.

I want to access the results from a call to coeftable() but I can't seem to as it does not have a mat field. My code based on your example looks like:

using RDatasets
using DataFramesMeta
using InstrumentalVariables
using DataFrames
cig = dataset("Ecdat", "Cigarette")
cig[:lrincome] = @with(cig, log(:Income./:POP./:CPI))
cig[:lrprice] = @with(cig, log(:AvgPrs./:CPI))
cig[:tdiff] = @with(cig, (:Taxs - :Tax)./:CPI)
cig[:rtax] = @with(cig, :Tax./:CPI)
cig95 = @ix(cig, :Year .== 1995)
y = convert(Array, log(cig95[:PackPC]))
x = convert(Array, [ones(size(cig95,1)) cig95[:lrincome] cig95[:lrprice]])
z = convert(Array, [ones(size(cig95,1)) cig95[:lrincome] cig95[:tdiff] cig95[:rtax]])
iv = ivreg(x, z, y)
res = coeftable(iv)
fieldnames(res)

which shows res has fields cols, colnms, and rownms, I was expecting a mat field to access the results from. If there was a mat of results I would then code something like:

res2 = DataFrame(variable = res.rownms,
               Estimate = res.mat[:,1],
               StdError = res.mat[:,2],
               z_val = res.mat[:,3],
               p_val = res.mat[:,4])

Thanks for any help Tom

gragusa commented 8 years ago

Doesn't cols contain what you need?

res2 = DataFrame(variable = res.rownms,
               Estimate = res.cols[1],
               StdError = res.cols[2],
               z_val = res.cols[3],
               p_val = res.cols[4])

gives

julia> res2 = DataFrame(variable = res.rownms,
                      Estimate = res.cols[1],
                      StdError = res.cols[2],
                      z_val = res.cols[3],
                      p_val = res.cols[4])
3×5 DataFrames.DataFrame
│ Row │ variable │ Estimate │ StdError │ z_val    │ p_val  │
├─────┼──────────┼──────────┼──────────┼──────────┼────────┤
│ 1   │ "x1"     │ 9.89496  │ 0.928758 │ 10.654   │ <1e-99 │
│ 2   │ "x2"     │ 0.280405 │ 0.245828 │ 1.14066  │ 0.0966 │
│ 3   │ "x3"     │ -1.27742 │ 0.241684 │ -5.28552 │ <1e-99 │
remlapmot commented 8 years ago

Thanks indeed - sorry I am new to Julia and don't yet understand it very well