2013-12-08 19 views
1

我想從命令中的 R包的輸出中提取標準錯誤。從tsls輸出中提取標準錯誤

使用一些通用的代碼作爲一個例子:

fit = tsls(Y ~ X, ~Z) 
summary(fit) 

摘要函數輸出除了迴歸估計幾件事情(例如,模型公式,殘差的摘要)。

我想要一個等效於的擬合$ coef輸出標準錯誤。但這似乎不是一種選擇。所有用於做glmlm等效的代碼在這裏似乎不起作用。有沒有辦法破解輸出?

回答

3

有時需要一點點挖掘才能找到這些值來自哪裏。如果你沒有從str(fit)得到任何線索,最好看的地方是看summary.tsls在做什麼。

getAnywhere("summary.tsls")一些幫助,我們看到了:

getAnywhere("summary.tsls") 
# A single object matching ‘summary.tsls’ was found 
# It was found in the following places 
# registered S3 method for summary from namespace sem 
# namespace:sem 
# with value 
# 
# function (object, digits = getOption("digits"), ...) 
# { 
# ### 
# ### \\\SNIP/// 
# ### 
#  std.errors <- sqrt(diag(object$V)) 
# ### 
# ### \\\SNIP/// 
# ### 
# } 
# <bytecode: 0x503c530> 
# <environment: namespace:sem> 

因此,要獲得您正在尋找的價值,你需要自己與計算的話:

sqrt(diag(fit$V)) 

一可重現的例子:

library(sem) 
fit <- tsls(Q ~ P + D, ~ D + F + A, data=Kmenta) 
summary(fit) 
# 
# 2SLS Estimates 
# 
# Model Formula: Q ~ P + D 
# 
# Instruments: ~D + F + A 
# 
# Residuals: 
# Min. 1st Qu. Median Mean 3rd Qu. Max. 
# -3.4300 -1.2430 -0.1895 0.0000 1.5760 2.4920 
# 
#    Estimate Std. Error t value Pr(>|t|)  
# (Intercept) 94.63330387 7.92083831 11.94738 1.0762e-09 *** 
# P   -0.24355654 0.09648429 -2.52431 0.021832 * 
# D   0.31399179 0.04694366 6.68869 3.8109e-06 *** 
# --- 
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
# 
# Residual standard error: 1.9663207 on 17 degrees of freedom 

sqrt(diag(fit$V)) 
# (Intercept)   P   D 
# 7.92083831 0.09648429 0.04694366 
+0

+1良好的解決方案。我不明白爲什麼函數'summary.tsls'的結果被打印,然後函數返回'NULL'。真是浪費時間...... –

相關問題