how to read output from structure command (str)

After running a pcr analysis using caret and train function, I obtain an object which I call model.

I need to know how to refer to number of components (Results-ncomp )so I can do a graph. I don't know how to refer to an item at second level (results followed by ncomp). I am enclosing output. Can you help?
Thanks.

List of 24
 $ method      : chr "pcr"
 $ modelInfo   :List of 12
  ..$ label     : chr "Principal Component Analysis"
  ..$ library   : chr "pls"
  ..$ type      : chr "Regression"
  ..$ parameters:'data.frame':	1 obs. of  3 variables:
  .. ..$ parameter: chr "ncomp"  
  .. ..$ class    : chr "numeric"
  .. ..$ label    : chr "#Components"
  ..$ grid      :function (x, y, len = NULL, search = "grid")  
  ..$ loop      :function (grid)  
  ..$ fit       :function (x, y, wts, param, lev, last, classProbs, ...)  
  ..$ predict   :function (modelFit, newdata, submodels = NULL)  
  ..$ predictors:function (x, ...)  
  ..$ tags      : chr [1:2] "Linear Regression" "Feature Extraction"
  ..$ prob      : NULL
  ..$ sort      :function (x)  
 $ modelType   : chr "Regression"
 $ results     :'data.frame':	10 obs. of  7 variables:
  ..$ ncomp     : num [1:10] 1 2 3 4 5 6 7 8 9 10      <---------------------------------------- ??????
  ..$ RMSE      : num [1:10] 4371 4347 4118 4118 4122 ...
  ..$ Rsquared  : num [1:10] 0.269 0.277 0.351 0.351 0.349 ...
  ..$ MAE       : num [1:10] 3495 3475 3263 3263 3269 ...
  ..$ RMSESD    : num [1:10] 122 123 130 129 127 ...
  ..$ RsquaredSD: num [1:10] 0.0455 0.0442 0.0415 0.0417 0.0415 ...
  ..$ MAESD     : num [1:10] 105 107 114 115 113 ...
 $ pred        : NULL
 $ bestTune    :'data.frame':	1 obs. of  1 variable:
  ..$ ncomp: num 10
 $ call        : language train.formula(form = Y ~ ., data = train, method = "pcr", scale = TRUE, trControl = trainControl("cv", number = 1| __truncated__
 $ dots        :List of 1
  ..$ scale: logi TRUE
 $ metric      : chr "RMSE"
 $ control     :List of 28
~~~

Using model$results$ncomp should work.

I had tried that a few times in the past with other data and it did not work. Thank you for your reply. I will continue to use the str command and hopefully use it more successfully in the future.

Can you recommend a study source (text or web link)?

Here is an example using the output of the lm() function. I truncated the output of str(model) to save space.

> DF <- data.frame(X = 1:10, Y = 1:10 + rnorm(10))
> model <- lm(Y ~ X, data = DF)
> str(model)
List of 12
 $ coefficients : Named num [1:2] 0.656 0.913
  ..- attr(*, "names")= chr [1:2] "(Intercept)" "X"
 $ residuals    : Named num [1:10] 0.0363 0.0697 -0.6425 0.7745 -1.0033 ...
  ..- attr(*, "names")= chr [1:10] "1" "2" "3" "4" ...
 $ effects      : Named num [1:10] -17.945 8.288 -0.659 0.764 -1.009 ...
  ..- attr(*, "names")= chr [1:10] "(Intercept)" "X" "" "" ...
 $ rank         : int 2
 $ fitted.values: Named num [1:10] 1.57 2.48 3.39 4.31 5.22 ...
  ..- attr(*, "names")= chr [1:10] "1" "2" "3" "4" ...
 $ assign       : int [1:2] 0 1
 $ qr           :List of 5
  ..$ qr   : num [1:10, 1:2] -3.162 0.316 0.316 0.316 0.316 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:10] "1" "2" "3" "4" ...
  .. .. ..$ : chr [1:2] "(Intercept)" "X"
  .. ..- attr(*, "assign")= int [1:2] 0 1
  ..$ qraux: num [1:2] 1.32 1.27
  ..$ pivot: int [1:2] 1 2
  ..$ tol  : num 1e-07
  ..$ rank : int 2
  ..- attr(*, "class")= chr "qr"
 $ df.residual  : int 8
[truncated]
> model$qr$qraux
[1] 1.316228 1.266308

Is that similar to what you tried?

I have been coding daily since August and don't really remember the applications where I tried this. I suspect it was not a str(model) but rather a str applied to a different type of object.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.