代码之家  ›  专栏  ›  技术社区  ›  Karl

R:访问字段值

  •  9
  • Karl  · 技术社区  · 16 年前

    我想知道如何访问R对象中包含的各个字段。或者,更准确地说,如何让R告诉我。

    例如,如果我运行以下代码:

    dx.ct <- ur.df(dat1[,'dx'], lags=3, type='trend')
    summary(dx.ct)
    

    然后我得到这个输出:

    ############################################### 
    # Augmented Dickey-Fuller Test Unit Root Test # 
    ############################################### 
    
    Test regression trend 
    
    
    Call:
    lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
    
    Residuals:
         Min       1Q   Median       3Q      Max 
    -0.46876 -0.24506  0.02420  0.15752  0.66688 
    
    Coefficients:
                 Estimate Std. Error t value Pr(>|t|)  
    (Intercept)  1.099231   0.561377   1.958   0.0606 .
    z.lag.1     -0.239438   0.141093  -1.697   0.1012  
    tt          -0.019831   0.007799  -2.543   0.0170 *
    z.diff.lag1 -0.306326   0.193001  -1.587   0.1241  
    z.diff.lag2 -0.214229   0.186135  -1.151   0.2599  
    z.diff.lag3 -0.223433   0.179040  -1.248   0.2228  
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
    
    Residual standard error: 0.3131 on 27 degrees of freedom
    Multiple R-squared: 0.3326,     Adjusted R-squared: 0.209 
    F-statistic: 2.691 on 5 and 27 DF,  p-value: 0.04244 
    
    
    Value of test-statistic is: -1.697 2.4118 3.2358 
    
    Critical values for test statistics: 
          1pct  5pct 10pct
    tau3 -4.15 -3.50 -3.18
    phi2  7.02  5.13  4.31
    phi3  9.31  6.73  5.61
    

    所以,我知道我应该能够单独访问上面的所有值,我不知道如何指向它们。有什么方法可以让R告诉我它们是如何存储的吗?

    我的思路是:

    showobjects(summary(dx.ct))
    

    然后输出

    $formula
    $residuals
    $coefficients
    etc.
    

    然后我就可以了

    showobjects(summary(dx.ct)$residuals)
    

    然后输出

    $min
    $1Q
    $median
    etc.
    

    谢谢
    卡尔

    4 回复  |  直到 13 年前
        1
  •  12
  •   Richie Cotton Joris Meys    16 年前

    作为另一种选择,看看 attributes . 例如

    example(ur.df)
    attributes(lc.df) #lc.df is an ur.df object created during by example.
    
    $y
    [1] 10.4831 10.4893 10.5022 10.5240 10.5329 10.5586 10.5190 10.5381
    [9] 10.5422 10.5361 10.5462 10.5459 10.5552 10.5548 10.5710 10.5861
    [17] 10.5864 10.5802 10.6006 10.6168 10.6275 10.6414 10.6629 10.6758
    [25] 10.6881 10.7240 10.7143 10.7222 10.7156 10.6964 10.6990 10.7081
    [33] 10.7142 10.7078 10.7073 10.6954 10.6910 10.6967 10.7015 10.7083
    [41] 10.7127 10.6922 10.6874 10.6989 10.7224 10.7452 10.7462 10.7663
    [49] 10.7633 10.7737 10.8282 10.7872 10.8015 10.8139 10.7909 10.8029
    [57] 10.7868 10.7979 10.8007 10.8008 10.7991 10.7956 10.8005 10.8160
    [65] 10.8260 10.8405 10.8482 10.8633 10.8633 10.8615 10.8732 10.8649
    [73] 10.8793 10.8909 10.8938 10.9116 10.9202 10.9409 10.9663 10.9700
    [81] 10.9808 10.9878 11.0048 11.0272 11.0420 11.0701 11.0751 11.0964
    [89] 11.1069 11.1123 11.1231 11.1223 11.1303 11.1307 11.1389 11.1325
    [97] 11.1261 11.1232 11.1220
    
    $model
    [1] "trend"
    
    $lags
    [1] 3
    
    # etc.
    

    如果你不想要完整的输出,那么 names(attributes(lc.df)) 只返回,嗯,名称。

    [1] "y"         "model"     "lags"      "cval"      "res"       "teststat"  "testreg"   "test.name" "class" 
    
        2
  •  6
  •   Dirk is no longer here    16 年前

    此外 unclass() 尝试 str() . 或者读取源代码以查看其他访问器是如何执行的。

    编辑: 例如,下面是S4类方法的源代码 summary 对于 ur.df 你看到的物体:

    setMethod("summary", "ur.df", function(object){
      return(new("sumurca", classname="ur.df", test.name=object@test.name,\
        testreg=object@testreg, teststat=object@teststat, cval=object@cval, \
        bpoint=NULL, signif=NULL, model=object@model, type=NULL, auxstat=NULL, \
        lag=NULL, H=NULL, A=NULL, lambda=NULL, pval=NULL, V=NULL, W=NULL, P=NULL))
    })
    

    它使用一个标准 @ S4对象元素的访问器。

        3
  •  5
  •   Shane    16 年前

    你能做的一件事就是利用 unclass() .

     ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
     trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
     group <- gl(2,10,20, labels=c("Ctl","Trt"))
     weight <- c(ctl, trt)
     anova(lm.D9 <- lm(weight ~ group))
     s <- summary(lm.D90 <- lm(weight ~ group - 1))
    

    使用名称进行调查:

     > names(unclass(s))
      [1] "call"          "terms"         "residuals"     "coefficients"  "aliased"            "sigma"         "df"            "r.squared"     "adj.r.squared"
     [10] "fstatistic"    "cov.unscaled" 
    

    然后引用一个特定值:

     > s$r.squared
     [1] 0.9817833
    
        4
  •  1
  •   sebastian-c    13 年前

    如Dirk Eddelbuettel所述,您可以通过@为此S4类型类获取指定的字段值。

    summary(dx.ct)@teststat
    
                   tau3      phi2     phi3
    statistic -1.668368 0.9731316 1.444461
    
    summary(dx.ct)@cval
    
    1pct  5pct 10pct
    tau3 -3.96 -3.41 -3.12
    phi2  6.09  4.68  4.03
    phi3  8.27  6.25  5.34