R testthat_result saving in file - r

I am trying to save unit tests result generated by "testthat" package result
How to save testthat result which is "testthat_result" object into a file(txt or csv file)
library(testthat)
result<-test_file('utils_test.R', reporter = "minimal")
outfile<-file("output.txt")
writeLines(c(result), outfile)
close(outfile)
But this is not working

The problem is your result object, which is a list.
library(testthat)
path <- testthat_example("success")
result<-test_file(path, reporter = "minimal")
str(result)
You need to convert it to a character stream before saving it using writeLines.
str(result)
List of 4
$ :List of 7
..$ file : chr "test-success.R"
..$ context: NULL
..$ test : chr "one plus one is two"
..$ user : num 0.006
..$ system : num 0
..$ real : num 0.006
..$ results:List of 1
.. ..$ :List of 6
.. .. ..$ message : chr "1 + 1 not equal to 2.\nEqual"
.. .. ..$ srcref : 'srcref' int [1:8] 2 3 2 24 3 24 2 2
.. .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x5589bd9e6a20>
.. .. ..$ trace : NULL
.. .. ..$ start_frame: int 31
.. .. ..$ end_frame : num 32
.. .. ..$ test : chr "one plus one is two"
.. .. ..- attr(*, "class")= chr [1:3] "expectation_success" "expectation" "condition"
$ :List of 7
..$ file : chr "test-success.R"
..$ context: NULL
..$ test : chr "you can skip tests if needed"
..$ user : num 0.003
..$ system : num 0
..$ real : num 0.003
..$ results:List of 1
.. ..$ :List of 6
.. .. ..$ message : chr "Reason: This tests hasn't been written yet"
.. .. ..$ srcref : 'srcref' int [1:8] 6 3 6 44 3 44 6 6
.. .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x5589bd9e6a20>
.. .. ..$ trace : NULL
.. .. ..$ start_frame: int 31
.. .. ..$ end_frame : num 31
.. .. ..$ test : chr "you can skip tests if needed"
.. .. ..- attr(*, "class")= chr [1:3] "expectation_skip" "expectation" "condition"
$ :List of 7
..$ file : chr "test-success.R"
..$ context: NULL
..$ test : chr "some tests have warnings"
..$ user : num 0.006
..$ system : num 0
..$ real : num 0.007
..$ results:List of 2
.. ..$ :List of 6
.. .. ..$ message : chr "NaNs produced"
.. .. ..$ srcref : 'srcref' int [1:8] 10 3 10 28 3 28 10 10
.. .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x5589bd9e6a20>
.. .. ..$ trace : NULL
.. .. ..$ start_frame: int 31
.. .. ..$ end_frame : num 33
.. .. ..$ test : chr "some tests have warnings"
.. .. ..- attr(*, "class")= chr [1:3] "expectation_warning" "expectation" "condition"
.. ..$ :List of 6
.. .. ..$ message : chr "log(-1) not equal to NaN.\nEqual"
.. .. ..$ srcref : 'srcref' int [1:8] 10 3 10 28 3 28 10 10
.. .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x5589bd9e6a20>
.. .. ..$ trace : NULL
.. .. ..$ start_frame: int 31
.. .. ..$ end_frame : num 32
.. .. ..$ test : chr "some tests have warnings"
.. .. ..- attr(*, "class")= chr [1:3] "expectation_success" "expectation" "condition"
$ :List of 7
..$ file : chr "test-success.R"
..$ context: NULL
..$ test : chr "some more successes just to pad things out"
..$ user : num 0.003
..$ system : num 0
..$ real : num 0.003
..$ results:List of 2
.. ..$ :List of 6
.. .. ..$ message : chr "TRUE isn't true."
.. .. ..$ srcref : 'srcref' int [1:8] 14 3 14 19 3 19 14 14
.. .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x5589bd9e6a20>
.. .. ..$ trace : NULL
.. .. ..$ start_frame: int 31
.. .. ..$ end_frame : num 32
.. .. ..$ test : chr "some more successes just to pad things out"
.. .. ..- attr(*, "class")= chr [1:3] "expectation_success" "expectation" "condition"
.. ..$ :List of 6
.. .. ..$ message : chr "FALSE isn't false."
.. .. ..$ srcref : 'srcref' int [1:8] 15 3 15 21 3 21 15 15
.. .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x5589bd9e6a20>
.. .. ..$ trace : NULL
.. .. ..$ start_frame: int 31
.. .. ..$ end_frame : num 32
.. .. ..$ test : chr "some more successes just to pad things out"
.. .. ..- attr(*, "class")= chr [1:3] "expectation_success" "expectation" "condition"
- attr(*, "class")= chr "testthat_results"

Related

Counting Data in dataframe of dataframes in R

Using R, I'm trying to summarize and count occurrences of a dataframe whose contents is other dataframes. It's multi-dimensional, in other words, and the number of items varies for each element in the dataframe. I'm not very experienced, so I hope I write this correctly:
data is at:
<https://pastebin.com/1f5bAi2s>
I'd like to count the times each resource occurs in the data, for example "Xbox One, NM2" or "PS4, NM2"
Thank in advance for the help.
It's not clear what you want from this nested list. Here's the structure returned by str:
str(dat)
'data.frame': 6 obs. of 9 variables:
$ _class : chr "allocation" "allocation" "allocation" "allocation" ...
$ name : chr "CK-76303" "CK-77297" "CK-77432" "CK-70045" ...
$ invoices :List of 6
..$ :'data.frame': 0 obs. of 0 variables
..$ :'data.frame': 0 obs. of 0 variables
..$ :'data.frame': 0 obs. of 0 variables
..$ :'data.frame': 0 obs. of 0 variables
..$ :'data.frame': 0 obs. of 0 variables
..$ :'data.frame': 0 obs. of 0 variables
$ items :List of 6
..$ :'data.frame': 1 obs. of 1 variable:
.. ..$ resource:'data.frame': 1 obs. of 4 variables:
.. .. ..$ _class : chr "resource"
.. .. ..$ oid : int 58739185
.. .. ..$ name : chr "Studio 1"
.. .. ..$ barcode: logi NA
..$ :'data.frame': 1 obs. of 1 variable:
.. ..$ resource:'data.frame': 1 obs. of 4 variables:
.. .. ..$ _class : chr "resource"
.. .. ..$ oid : int 58739172
.. .. ..$ name : chr "Studio 1"
.. .. ..$ barcode: logi NA
..$ :'data.frame': 1 obs. of 1 variable:
.. ..$ resource:'data.frame': 1 obs. of 4 variables:
.. .. ..$ _class : chr "resource"
.. .. ..$ oid : int 1872017
.. .. ..$ name : chr "Drill Bit "
.. .. ..$ barcode: chr "1721.019"
..$ :'data.frame': 9 obs. of 1 variable:
.. ..$ resource: logi NA NA NA NA NA NA ...
..$ :'data.frame': 19 obs. of 1 variable:
.. ..$ resource:'data.frame': 19 obs. of 4 variables:
.. .. ..$ _class : chr "resource" "resource" "resource" "resource" ...
.. .. ..$ oid : int 61437789 61437748 61437699 61437651 58694528 58694527 58694526 58694525 58694524 58694523 ...
.. .. ..$ name : chr "Xbox One, NM2 " "Xbox One, NM2 " "PS4, NM2 " "PS4, NM2 " ...
.. .. ..$ barcode: logi NA NA NA NA NA NA ...
..$ :'data.frame': 1 obs. of 1 variable:
.. ..$ resource:'data.frame': 1 obs. of 4 variables:
.. .. ..$ _class : chr "resource"
.. .. ..$ oid : int 41390788
.. .. ..$ name : chr "Model Shop"
.. .. ..$ barcode: chr "IND2.1.004"
$ allocationContentsSummary:'data.frame': 6 obs. of 2 variables:
..$ groups:List of 6
.. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. ..$ label: chr "Reservation Canceled"
.. .. ..$ items:List of 1
.. .. .. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. .. .. ..$ label : chr " Studios 20"
.. .. .. .. ..$ subitems:List of 1
.. .. .. .. .. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. .. .. .. .. ..$ label: chr "Studio 1"
.. .. .. .. .. .. ..$ due : logi NA
.. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. ..$ label: chr "Reservation Canceled"
.. .. ..$ items:List of 1
.. .. .. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. .. .. ..$ label : chr "Studios 20"
.. .. .. .. ..$ subitems:List of 1
.. .. .. .. .. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. .. .. .. .. ..$ label: chr "Studio 1"
.. .. .. .. .. .. ..$ due : logi NA
.. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. ..$ label: chr "Returned"
.. .. ..$ items:List of 1
.. .. .. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. .. .. ..$ label : chr "Drill Bits "
.. .. .. .. ..$ subitems:List of 1
.. .. .. .. .. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. .. .. .. .. ..$ label: chr "Drill Bit_High "
.. .. .. .. .. .. ..$ due : logi NA
.. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. ..$ label: chr "Reservation Canceled"
.. .. ..$ items:List of 1
.. .. .. ..$ :'data.frame': 2 obs. of 2 variables:
.. .. .. .. ..$ label : chr "Music Classrom 110" "Music Studio 113 Vocal Tracking"
.. .. .. .. ..$ subitems:List of 2
.. .. .. .. .. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. .. .. .. .. ..$ label: chr "8 by type"
.. .. .. .. .. .. ..$ due : logi NA
.. .. .. .. .. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. .. .. .. .. ..$ label: chr "1 by type"
.. .. .. .. .. .. ..$ due : logi NA
.. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. ..$ label: chr "Reservation Canceled"
.. .. ..$ items:List of 1
.. .. .. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. .. .. ..$ label : chr "eSpiLounge"
.. .. .. .. ..$ subitems:List of 1
.. .. .. .. .. ..$ :'data.frame': 19 obs. of 2 variables:
.. .. .. .. .. .. ..$ label: chr "Nintendo, NM2 " "Nintendo, NM2 " "PC-01GAM" "PC-02GAM" ...
.. .. .. .. .. .. ..$ due : logi NA NA NA NA NA NA ...
.. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. ..$ label: chr "Reservation Canceled"
.. .. ..$ items:List of 1
.. .. .. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. .. .. ..$ label : chr "ModelS "
.. .. .. .. ..$ subitems:List of 1
.. .. .. .. .. ..$ :'data.frame': 1 obs. of 2 variables:
.. .. .. .. .. .. ..$ label: chr "ModelS_04"
.. .. .. .. .. .. ..$ due : logi NA
..$ lock : logi NA NA NA NA NA NA
$ itemCount : int 1 1 1 9 19 1
$ patronAcademicLevel : chr NA "BFA/BFA-PH" "BFA/BFA-JEM" "10" ...
$ realEndTime : chr NA NA "2022-05-11T12:21:00.000000-07:00" NA ...
$ realStartTime : chr NA NA "2022-05-11T08:58:00.000000-07:00" NA ...
Perhaps you want the items in the "items" node: Within each of those 6 dataframes there are character values that are each in the [[1]]$name leaves of those enclosing dataframe structures:
sapply(dat$items, function(x) x[[1]]['name'])
$name
[1] "Studio 1"
$name
[1] "Studio 1"
$name
[1] "Drill Bit "
[[4]]
[1] NA
$name
[1] "Xbox One, NM2 " "Xbox One, NM2 " "PS4, NM2 " "PS4, NM2 " "PC, NM2" "PC, NM2"
[7] "PC, NM2" "PC, NM2-" "PC, NM2" "Nintendo, NM2 " "Nintendo, NM2 "
$name
[1] "Model Shop"

How to handle list of lm objects or how to circumvent?

I have a data frame APD which consists of a factor (Serial_number) and each element of this factor has some observables (Amplification against Voltage). Now I would like to apply the function invest from the investr package to do an inverse regression across all serial numbers (means an inverse regression separated for each serial number).
This works very well if I only use the dataset of one Serial_number, e.g.:
> model<- lm(Amplification ~ Voltage + I(Voltage^2), data = APD[APD$Serial_number==608004648,])
> inverse<- invest(model, y0=150, interval=c("inversion", "Wald", "percentile", "none"),
level=0.95, mean.response = FALSE
)
> inverse
estimate lower upper
385.4670 385.0665 385.8446
When I take the whole data frame:
> models<- dlply(APD, "Serial_number", function(df) lm(Amplification ~ Voltage + I(Voltage^2), data = df))
> inverse<- invest(models, y0=150, interval=c("inversion", "Wald", "percentile", "none"),
level=0.95, mean.response = FALSE
)
> Error in UseMethod("invest") :
no applicable method for 'invest' applied to an object of class "list"
I tried inverse<- invest(models[1], y0=150, interval=c("inversion", "Wald", "percentile", "none") but it's the same problem.
I can't get my head around how to proceed further. I think this must be such a daily issue there are probably already practical solutions present. Don't they?
My next step would be to create a lm-object for each Serial_number but also this seems unnecessary complicated.
> model<- lm(Amplification ~ Voltage + I(Voltage^2), data = APD[APD$Serial_number==608004648,])
> str(model)
List of 12
$ coefficients : Named num [1:3] 59714.045 -318.043 0.424
..- attr(*, "names")= chr [1:3] "(Intercept)" "Voltage" "I(Voltage^2)"
$ residuals : Named num [1:4] -0.0483 0.1458 -0.1459 0.0484
..- attr(*, "names")= chr [1:4] "1" "2" "3" "4"
$ effects : Named num [1:4] -295.61 -38.412 3.412 -0.217
..- attr(*, "names")= chr [1:4] "(Intercept)" "Voltage" "I(Voltage^2)" ""
$ rank : int 3
$ fitted.values: Named num [1:4] 124 138 155 175
..- attr(*, "names")= chr [1:4] "1" "2" "3" "4"
$ assign : int [1:3] 0 1 2
$ qr :List of 5
..$ qr : num [1:4, 1:3] -2 0.5 0.5 0.5 -770 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:4] "1" "2" "3" "4"
.. .. ..$ : chr [1:3] "(Intercept)" "Voltage" "I(Voltage^2)"
.. ..- attr(*, "assign")= int [1:3] 0 1 2
..$ qraux: num [1:3] 1.5 1 1.37
..$ pivot: int [1:3] 1 2 3
..$ tol : num 1e-07
..$ rank : int 3
..- attr(*, "class")= chr "qr"
$ df.residual : int 1
$ xlevels : Named list()
$ call : language lm(formula = Amplification ~ Voltage + I(Voltage^2), data = APD[APD$Serial_number == 608004648, ])
$ terms :Classes 'terms', 'formula' language Amplification ~ Voltage + I(Voltage^2)
.. ..- attr(*, "variables")= language list(Amplification, Voltage, I(Voltage^2))
.. ..- attr(*, "factors")= int [1:3, 1:2] 0 1 0 0 0 1
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:3] "Amplification" "Voltage" "I(Voltage^2)"
.. .. .. ..$ : chr [1:2] "Voltage" "I(Voltage^2)"
.. ..- attr(*, "term.labels")= chr [1:2] "Voltage" "I(Voltage^2)"
.. ..- attr(*, "order")= int [1:2] 1 1
.. ..- attr(*, "intercept")= int 1
.. ..- attr(*, "response")= int 1
.. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. ..- attr(*, "predvars")= language list(Amplification, Voltage, I(Voltage^2))
.. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "numeric" "numeric"
.. .. ..- attr(*, "names")= chr [1:3] "Amplification" "Voltage" "I(Voltage^2)"
$ model :'data.frame': 4 obs. of 3 variables:
..$ Amplification: num [1:4] 124 138 155 175
..$ Voltage : num [1:4] 382 384 386 388
..$ I(Voltage^2) :Class 'AsIs' num [1:4] 145900 147440 148975 150528
..- attr(*, "terms")=Classes 'terms', 'formula' language Amplification ~ Voltage + I(Voltage^2)
.. .. ..- attr(*, "variables")= language list(Amplification, Voltage, I(Voltage^2))
.. .. ..- attr(*, "factors")= int [1:3, 1:2] 0 1 0 0 0 1
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:3] "Amplification" "Voltage" "I(Voltage^2)"
.. .. .. .. ..$ : chr [1:2] "Voltage" "I(Voltage^2)"
.. .. ..- attr(*, "term.labels")= chr [1:2] "Voltage" "I(Voltage^2)"
.. .. ..- attr(*, "order")= int [1:2] 1 1
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "response")= int 1
.. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. ..- attr(*, "predvars")= language list(Amplification, Voltage, I(Voltage^2))
.. .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "numeric" "numeric"
.. .. .. ..- attr(*, "names")= chr [1:3] "Amplification" "Voltage" "I(Voltage^2)"
- attr(*, "class")= chr "lm"
str(models) are hundreds of lines.. a few are:
models<- dlply(APD, "Serial_number", function(df) lm(Amplification ~ Voltage + I(Voltage^2), data = df))
str(models)
(...)
$ 713006445 :List of 12
..$ coefficients : Named num [1:3] 68847.991 -386.479 0.543
.. ..- attr(*, "names")= chr [1:3] "(Intercept)" "Voltage" "I(Voltage^2)"
..$ residuals : Named num [1:4] -0.0655 0.1964 -0.1962 0.0653
.. ..- attr(*, "names")= chr [1:4] "1" "2" "3" "4"
..$ effects : Named num [1:4] -317.503 45.226 4.347 0.293
.. ..- attr(*, "names")= chr [1:4] "(Intercept)" "Voltage" "I(Voltage^2)" ""
..$ rank : int 3
..$ fitted.values: Named num [1:4] 131 146 167 191
.. ..- attr(*, "names")= chr [1:4] "1" "2" "3" "4"
..$ assign : int [1:3] 0 1 2
..$ qr :List of 5
.. ..$ qr : num [1:4, 1:3] -2 0.5 0.5 0.5 -730.1 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:4] "1" "2" "3" "4"
.. .. .. ..$ : chr [1:3] "(Intercept)" "Voltage" "I(Voltage^2)"
.. .. ..- attr(*, "assign")= int [1:3] 0 1 2
.. ..$ qraux: num [1:3] 1.5 1 1.96
.. ..$ pivot: int [1:3] 1 2 3
.. ..$ tol : num 1e-07
.. ..$ rank : int 3
.. ..- attr(*, "class")= chr "qr"
..$ df.residual : int 1
..$ xlevels : Named list()
..$ call : language lm(formula = Amplification ~ Voltage + I(Voltage^2), data = df)
..$ terms :Classes 'terms', 'formula' language Amplification ~ Voltage + I(Voltage^2)
.. .. ..- attr(*, "variables")= language list(Amplification, Voltage, I(Voltage^2))
.. .. ..- attr(*, "factors")= int [1:3, 1:2] 0 1 0 0 0 1
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:3] "Amplification" "Voltage" "I(Voltage^2)"
.. .. .. .. ..$ : chr [1:2] "Voltage" "I(Voltage^2)"
.. .. ..- attr(*, "term.labels")= chr [1:2] "Voltage" "I(Voltage^2)"
.. .. ..- attr(*, "order")= int [1:2] 1 1
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "response")= int 1
.. .. ..- attr(*, ".Environment")=<environment: 0xb810100>
.. .. ..- attr(*, "predvars")= language list(Amplification, Voltage, I(Voltage^2))
.. .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "numeric" "numeric"
.. .. .. ..- attr(*, "names")= chr [1:3] "Amplification" "Voltage" "I(Voltage^2)"
..$ model :'data.frame': 4 obs. of 3 variables:
.. ..$ Amplification: num [1:4] 131 147 166 191
.. ..$ Voltage : num [1:4] 362 364 366 368
.. ..$ I(Voltage^2) :Class 'AsIs' num [1:4] 131064 132516 133974 135445
.. ..- attr(*, "terms")=Classes 'terms', 'formula' language Amplification ~ Voltage + I(Voltage^2)
.. .. .. ..- attr(*, "variables")= language list(Amplification, Voltage, I(Voltage^2))
.. .. .. ..- attr(*, "factors")= int [1:3, 1:2] 0 1 0 0 0 1
.. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. ..$ : chr [1:3] "Amplification" "Voltage" "I(Voltage^2)"
.. .. .. .. .. ..$ : chr [1:2] "Voltage" "I(Voltage^2)"
.. .. .. ..- attr(*, "term.labels")= chr [1:2] "Voltage" "I(Voltage^2)"
.. .. .. ..- attr(*, "order")= int [1:2] 1 1
.. .. .. ..- attr(*, "intercept")= int 1
.. .. .. ..- attr(*, "response")= int 1
.. .. .. ..- attr(*, ".Environment")=<environment: 0xb810100>
.. .. .. ..- attr(*, "predvars")= language list(Amplification, Voltage, I(Voltage^2))
.. .. .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "numeric" "numeric"
.. .. .. .. ..- attr(*, "names")= chr [1:3] "Amplification" "Voltage" "I(Voltage^2)"
..- attr(*, "class")= chr "lm"
$ 713006446 :List of 12
..$ coefficients : Named num [1:3] 58059.34 -331.908 0.475
.. ..- attr(*, "names")= chr [1:3] "(Intercept)" "Voltage" "I(Voltage^2)"
..$ residuals : Named num [1:4] -0.0574 0.1729 -0.1727 0.0573
.. ..- attr(*, "names")= chr [1:4] "1" "2" "3" "4"
..$ effects : Named num [1:4] -300.53 -41.391 3.808 -0.258
.. ..- attr(*, "names")= chr [1:4] "(Intercept)" "Voltage" "I(Voltage^2)" ""
..$ rank : int 3
..$ fitted.values: Named num [1:4] 124 139 158 180
.. ..- attr(*, "names")= chr [1:4] "1" "2" "3" "4"
..$ assign : int [1:3] 0 1 2
..$ qr :List of 5
.. ..$ qr : num [1:4, 1:3] -2 0.5 0.5 0.5 -717.9 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:4] "1" "2" "3" "4"
.. .. .. ..$ : chr [1:3] "(Intercept)" "Voltage" "I(Voltage^2)"
.. .. ..- attr(*, "assign")= int [1:3] 0 1 2
.. ..$ qraux: num [1:3] 1.5 1 1.37
.. ..$ pivot: int [1:3] 1 2 3
.. ..$ tol : num 1e-07
.. ..$ rank : int 3
.. ..- attr(*, "class")= chr "qr"
..$ df.residual : int 1
..$ xlevels : Named list()
..$ call : language lm(formula = Amplification ~ Voltage + I(Voltage^2), data = df)
..$ terms :Classes 'terms', 'formula' language Amplification ~ Voltage + I(Voltage^2)
.. .. ..- attr(*, "variables")= language list(Amplification, Voltage, I(Voltage^2))
.. .. ..- attr(*, "factors")= int [1:3, 1:2] 0 1 0 0 0 1
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:3] "Amplification" "Voltage" "I(Voltage^2)"
.. .. .. .. ..$ : chr [1:2] "Voltage" "I(Voltage^2)"
.. .. ..- attr(*, "term.labels")= chr [1:2] "Voltage" "I(Voltage^2)"
.. .. ..- attr(*, "order")= int [1:2] 1 1
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "response")= int 1
.. .. ..- attr(*, ".Environment")=<environment: 0xb6fc150>
.. .. ..- attr(*, "predvars")= language list(Amplification, Voltage, I(Voltage^2))
.. .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "numeric" "numeric"
.. .. .. ..- attr(*, "names")= chr [1:3] "Amplification" "Voltage" "I(Voltage^2)"
..$ model :'data.frame': 4 obs. of 3 variables:
.. ..$ Amplification: num [1:4] 124 139 157 180
.. ..$ Voltage : num [1:4] 356 358 360 362
.. ..$ I(Voltage^2) :Class 'AsIs' num [1:4] 126715 128145 129576 131024
.. ..- attr(*, "terms")=Classes 'terms', 'formula' language Amplification ~ Voltage + I(Voltage^2)
.. .. .. ..- attr(*, "variables")= language list(Amplification, Voltage, I(Voltage^2))
.. .. .. ..- attr(*, "factors")= int [1:3, 1:2] 0 1 0 0 0 1
.. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. ..$ : chr [1:3] "Amplification" "Voltage" "I(Voltage^2)"
.. .. .. .. .. ..$ : chr [1:2] "Voltage" "I(Voltage^2)"
.. .. .. ..- attr(*, "term.labels")= chr [1:2] "Voltage" "I(Voltage^2)"
.. .. .. ..- attr(*, "order")= int [1:2] 1 1
.. .. .. ..- attr(*, "intercept")= int 1
.. .. .. ..- attr(*, "response")= int 1
.. .. .. ..- attr(*, ".Environment")=<environment: 0xb6fc150>
.. .. .. ..- attr(*, "predvars")= language list(Amplification, Voltage, I(Voltage^2))
.. .. .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "numeric" "numeric"
.. .. .. .. ..- attr(*, "names")= chr [1:3] "Amplification" "Voltage" "I(Voltage^2)"
..- attr(*, "class")= chr "lm"
$ 713006449 :List of 12
..$ coefficients : Named num [1:3] 56708.406 -324.219 0.464
(...)
I use RStudio/Ubuntu 14.04.
edit: Due to advices of joran and Gregor I tried the following:
> invest(models[[1]], y0=150, interval=c("inversion", "Wald", "percentile", "none"), level=0.95, mean.response = FALSE)
Error in invest.lm(models[[1]], y0 = 150, interval = c("inversion", "Wald", :
'x0.name' is missing, please select a valid predictor variable
and
in case of lapply I'm not sure how to define it > lapply(X, FUN, ...) and in my case this would be lapply(object, invest(object, ..)) :
> lapply(models, invest(models, y0=150, interval=c("inversion", "Wald", "percentile", "none"), level=0.95, mean.response = FALSE))
Error in UseMethod("invest") :
no applicable method for 'invest' applied to an object of class "list"
editedit:
I tried to use a lmList as follows:
library(nlme)
models<- lmList(Amplification ~ Voltage + I(Voltage^2)|Serial_number, data= APD)
> invest(models[[1]], y0=150, interval=c("inversion", "Wald", "percentile", "none"),
+ level=0.95, mean.response = FALSE)
Error in eval(object$call$data, envir = parent.frame()) :
object 'dat' not found
Has someone an idea how to proceed? I take every solution which allows to apply a lm fit to the factor of a dataset and then to apply the invest function to each lm object.
Isn't there the possibility to extract a lm object out of a list of lm objects and it remains as an lm object?
In case someone else will have a hard time solving this "trivial" issue: I circumvented the scenario above by splitting the data frame into many data frames separated by the levels of the factor:
new_data_frame<- split(data_frame, data_frame$factor)
Next, I iterate through all data frames and fit and invest each:
for(i in 1:length(new_data_frame)){
temp_data_frame<- as.data.frame(new_data_frame[i])
fit<- lm(Response ~ Predictor, data = temp_model_frame)
inverse_regression<- invest(fit, y0=150, interval=c("inversion", "Wald", "percentile", "none"), level=0.95, mean.response = FALSE)
# some lines to store each time inverse_regression..
}

R: Coeftest causes error

I am performing a Newey-West test to assess an estimator of a regression with heteroskedastic and autocorrelated residuals.
I am using the "sandwich" and "lmtest" packages.
While I can easily reproduce examples found on other sites, my own script causes the error:
Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) : 'dimnames' applied to non-array
My code:
ffregression <- lm(ex.return ~ fff$Mkt.RF + fff$SMB + fff$HML)
coeftest(ffregression,vcov=NeweyWest)
str(ffregression):
List of 12
$ coefficients : Named num [1:4] 0.00604 0.72976 0.90351 0.13548
..- attr(*, "names")= chr [1:4] "(Intercept)" "fff$Mkt.RF" "fff$SMB" "fff$HML"
$ residuals :An ‘xts’ object on Mar 2014/Dec 2016 containing:
Data: num [1:34] -0.03637 0.0408 -0.00672 0.04648 -0.02275 ...
Indexed by objects of class: [yearmon] TZ:
Original class: 'double'
xts Attributes:
NULL
$ effects :An ‘xts’ object on Mar 2014/Dec 2016 containing:
Data: num [1:34] -0.0606 0.1785 0.1379 0.0204 -0.0262 ...
Indexed by objects of class: [yearmon] TZ:
Original class: 'double'
xts Attributes:
NULL
$ rank : int 4
$ fitted.values:An ‘xts’ object on Mar 2014/Dec 2016 containing:
Data: num [1:34] -0.000725 -0.031716 0.003868 0.051386 -0.047005 ...
Indexed by objects of class: [yearmon] TZ:
Original class: 'double'
xts Attributes:
NULL
$ assign : int [1:4] 0 1 2 3
$ qr :List of 5
..$ qr : num [1:34, 1:4] -5.831 0.171 0.171 0.171 0.171 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:34] "1" "2" "3" "4" ...
.. .. ..$ : chr [1:4] "(Intercept)" "fff$Mkt.RF" "fff$SMB" "fff$HML"
.. ..- attr(*, "assign")= int [1:4] 0 1 2 3
..$ qraux: num [1:4] 1.17 1.05 1.14 1.14
..$ pivot: int [1:4] 1 2 3 4
..$ tol : num 1e-07
..$ rank : int 4
..- attr(*, "class")= chr "qr"
$ df.residual : int 30
$ xlevels : Named list()
$ call : language lm(formula = ex.return ~ fff$Mkt.RF + fff$SMB + fff$HML)
$ terms :Classes 'terms', 'formula' language ex.return ~ fff$Mkt.RF + fff$SMB + fff$HML
.. ..- attr(*, "variables")= language list(ex.return, fff$Mkt.RF, fff$SMB, fff$HML)
.. ..- attr(*, "factors")= int [1:4, 1:3] 0 1 0 0 0 0 1 0 0 0 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:4] "ex.return" "fff$Mkt.RF" "fff$SMB" "fff$HML"
.. .. .. ..$ : chr [1:3] "fff$Mkt.RF" "fff$SMB" "fff$HML"
.. ..- attr(*, "term.labels")= chr [1:3] "fff$Mkt.RF" "fff$SMB" "fff$HML"
.. ..- attr(*, "order")= int [1:3] 1 1 1
.. ..- attr(*, "intercept")= int 1
.. ..- attr(*, "response")= int 1
.. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. ..- attr(*, "predvars")= language list(ex.return, fff$Mkt.RF, fff$SMB, fff$HML)
.. ..- attr(*, "dataClasses")= Named chr [1:4] "nmatrix.1" "nmatrix.1" "nmatrix.1" "nmatrix.1"
.. .. ..- attr(*, "names")= chr [1:4] "ex.return" "fff$Mkt.RF" "fff$SMB" "fff$HML"
$ model :'data.frame': 34 obs. of 4 variables:
..$ ex.return :An ‘xts’ object on Mar 2014/Dec 2016 containing:
Data: num [1:34, 1] -0.03709 0.00909 -0.00285 0.09786 -0.06975 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr "RF"
Indexed by objects of class: [yearmon] TZ:
Original class: 'double'
xts Attributes:
NULL
..$ fff$Mkt.RF:An ‘xts’ object on Mar 2014/Dec 2016 containing:
Data: num [1:34, 1] 0.0043 -0.0019 0.0206 0.0261 -0.0204 0.0424 -0.0197 0.0252 0.0255 -0.0006 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr "Mkt.RF"
Indexed by objects of class: [yearmon] TZ:
xts Attributes:
NULL
..$ fff$SMB :An ‘xts’ object on Mar 2014/Dec 2016 containing:
Data: num [1:34, 1] -0.0185 -0.0419 -0.0185 0.0301 -0.0422 0.004 -0.038 0.0428 -0.0205 0.0259 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr "SMB"
Indexed by objects of class: [yearmon] TZ:
xts Attributes:
NULL
..$ fff$HML :An ‘xts’ object on Mar 2014/Dec 2016 containing:
Data: num [1:34, 1] 0.0503 0.011 -0.0036 -0.0066 -0.0002 -0.0055 -0.0119 -0.0168 -0.0298 0.0212 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr "HML"
Indexed by objects of class: [yearmon] TZ:
xts Attributes:
NULL
..- attr(*, "terms")=Classes 'terms', 'formula' language ex.return ~ fff$Mkt.RF + fff$SMB + fff$HML
.. .. ..- attr(*, "variables")= language list(ex.return, fff$Mkt.RF, fff$SMB, fff$HML)
.. .. ..- attr(*, "factors")= int [1:4, 1:3] 0 1 0 0 0 0 1 0 0 0 ...
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:4] "ex.return" "fff$Mkt.RF" "fff$SMB" "fff$HML"
.. .. .. .. ..$ : chr [1:3] "fff$Mkt.RF" "fff$SMB" "fff$HML"
.. .. ..- attr(*, "term.labels")= chr [1:3] "fff$Mkt.RF" "fff$SMB" "fff$HML"
.. .. ..- attr(*, "order")= int [1:3] 1 1 1
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "response")= int 1
.. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. ..- attr(*, "predvars")= language list(ex.return, fff$Mkt.RF, fff$SMB, fff$HML)
.. .. ..- attr(*, "dataClasses")= Named chr [1:4] "nmatrix.1" "nmatrix.1" "nmatrix.1" "nmatrix.1"
.. .. .. ..- attr(*, "names")= chr [1:4] "ex.return" "fff$Mkt.RF" "fff$SMB" "fff$HML"
- attr(*, "class")= chr "lm"
Example:
set.seed(04012017)
n<-34
correlated_residuals<-arima.sim(list(ar = .9), n)
y<-correlated_residuals
x<-1:n
plot(x,correlated_residuals)
fit<-lm(y~x)
abline(fit)
summary(fit) # standard estimates
coeftest(fit,vcov=NeweyWest(fit,verbose=T))
str(fit):
List of 12
$ coefficients : Named num [1:2] -0.179 0.148
..- attr(*, "names")= chr [1:2] "(Intercept)" "x"
$ residuals : Named num [1:34] -0.9529 0.976 0.3025 -0.0486 -1.1214 ...
..- attr(*, "names")= chr [1:34] "1" "2" "3" "4" ...
$ effects : Named num [1:34] -14.026 8.449 0.25 -0.085 -1.142 ...
..- attr(*, "names")= chr [1:34] "(Intercept)" "x" "" "" ...
$ rank : int 2
$ fitted.values: Named num [1:34] -0.0314 0.1163 0.264 0.4116 0.5593 ...
..- attr(*, "names")= chr [1:34] "1" "2" "3" "4" ...
$ assign : int [1:2] 0 1
$ qr :List of 5
..$ qr : num [1:34, 1:2] -5.831 0.171 0.171 0.171 0.171 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:34] "1" "2" "3" "4" ...
.. .. ..$ : chr [1:2] "(Intercept)" "x"
.. ..- attr(*, "assign")= int [1:2] 0 1
..$ qraux: num [1:2] 1.17 1.23
..$ pivot: int [1:2] 1 2
..$ tol : num 1e-07
..$ rank : int 2
..- attr(*, "class")= chr "qr"
$ df.residual : int 32
$ xlevels : Named list()
$ call : language lm(formula = y ~ x)
$ terms :Classes 'terms', 'formula' language y ~ x
.. ..- attr(*, "variables")= language list(y, x)
.. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:2] "y" "x"
.. .. .. ..$ : chr "x"
.. ..- attr(*, "term.labels")= chr "x"
.. ..- attr(*, "order")= int 1
.. ..- attr(*, "intercept")= int 1
.. ..- attr(*, "response")= int 1
.. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. ..- attr(*, "predvars")= language list(y, x)
.. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. ..- attr(*, "names")= chr [1:2] "y" "x"
$ model :'data.frame': 34 obs. of 2 variables:
..$ y: num [1:34] -0.984 1.092 0.566 0.363 -0.562 ...
..$ x: int [1:34] 1 2 3 4 5 6 7 8 9 10 ...
..- attr(*, "terms")=Classes 'terms', 'formula' language y ~ x
.. .. ..- attr(*, "variables")= language list(y, x)
.. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:2] "y" "x"
.. .. .. .. ..$ : chr "x"
.. .. ..- attr(*, "term.labels")= chr "x"
.. .. ..- attr(*, "order")= int 1
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "response")= int 1
.. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. ..- attr(*, "predvars")= language list(y, x)
.. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. .. ..- attr(*, "names")= chr [1:2] "y" "x"
- attr(*, "class")= chr "lm"
It seems that R is complaining that it cannot assign dimnames because your data is coming from a non-array. Try to use only one xts object for your data instead.
fff$ex.return <- ex.return
ffregression <- lm(ex.return ~ Mkt.RF + SMB + HML, data = fff)
coeftest(ffregression,vcov=NeweyWest)

access the data inside the slots of S4 objects

basically I am using a package that gives me a S4 object with many slots. these objects are easily accessible with s4obj#portfolio but then I need to extract FROM this slot a vector.
more specifically, the package is fPortfolio and the function is portfolioFrontier(). in the pdf associated is at page 19.
(http://postimg.org/image/62oa8z7dv/)
in the pdf attached to the package it is specfied that such slot is
"a list, containing parameter specifications for the portfolio:
weights a numeric vector specifying the portfolio weights,
targetReturn a numeric value specifying the target return, "
I want to carve out "weights" but nothing worked so far (errors include that object is not subsettable, $ doens't work [meaning s4obj#portfolio$weights doesn't work], etc)
code at work:
frontier=portfolioFrontier(as.timeSeries(elements))
frontier#portfolio
frontier#portfolio$weights
Error in frontier#portfolio$weights :
$ operator not defined for this S4 class
p.s.: i hope this isn't already answered elsewhere but I found nothing on this forum or the web for that matter
p.s.2 : str() yields
> str(frontier#portfolio)
Formal class 'fPFOLIOVAL' [package "fPortfolio"] with 2 slots
..# portfolio:List of 7
.. ..$ weights : num [1:49, 1:14] 0.0805 0.161 0.2415 0.322 0.4025 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. ..$ covRiskBudgets : num [1:49, 1:14] -3.54e-05 -7.73e-05 -1.28e-04 -1.90e-04 -2.67e-04 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. ..$ targetReturn : num [1:49, 1:2] -0.00171 -0.00154 -0.00136 -0.00119 -0.00101 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:2] "mean" "mu"
.. ..$ targetRisk : num [1:49, 1:4] 0.0394 0.0359 0.0325 0.029 0.0256 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:4] "Cov" "Sigma" "CVaR" "VaR"
.. ..$ targetAlpha : num 0.05
.. ..$ minriskPortfolio:Formal class 'fPORTFOLIO' [package "fPortfolio"] with 7 slots
.. .. .. ..# call : language minriskPortfolio(data = data, spec = spec, constraints = constraints)
.. .. .. ..# data :Formal class 'fPFOLIODATA' [package "fPortfolio"] with 3 slots
.. .. .. .. .. ..# data :List of 3
.. .. .. .. .. .. ..$ series :Time Series:
Name: object
Data Matrix:
Dimension: 240 14
Column Names: MNEU OBGVEUBR OBGVEUML OBCPEU OBGVIN OBCPNOEU OBGLHGYD OBPSEM AZEU AZUS AZPC AZEM AZRE ATCOMM
Row Names: ...
Positions:
Start:
End:
With:
Format: counts
FinCenter:
Units: MNEU OBGVEUBR OBGVEUML OBCPEU OBGVIN OBCPNOEU OBGLHGYD OBPSEM AZEU AZUS AZPC AZEM AZRE ATCOMM
Title: Signal Series Object
Documentation: Wed Mar 18 12:18:11 2015
.. .. .. .. .. .. ..$ nAssets: int 14
.. .. .. .. .. .. ..$ names : chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. ..# statistics:List of 5
.. .. .. .. .. .. ..$ mean : Named num [1:14] 0.000277 0.0011 0.003337 0.002433 0.001609 ...
.. .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. .. ..$ Cov : num [1:14, 1:14] 2.57e-08 1.60e-07 1.39e-07 -7.50e-08 4.11e-07 ...
.. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. .. .. .. ..$ : chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. .. .. .. ..$ : chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. .. ..$ estimator: chr "covEstimator"
.. .. .. .. .. .. ..$ mu : Named num [1:14] 0.000277 0.0011 0.003337 0.002433 0.001609 ...
.. .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. .. ..$ Sigma : num [1:14, 1:14] 2.57e-08 1.60e-07 1.39e-07 -7.50e-08 4.11e-07 ...
.. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. .. .. .. ..$ : chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. .. .. .. ..$ : chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. ..# tailRisk : list()
.. .. .. ..# spec :Formal class 'fPFOLIOSPEC' [package "fPortfolio"] with 5 slots
.. .. .. .. .. ..# model :List of 5
.. .. .. .. .. .. ..$ type : chr "MV"
.. .. .. .. .. .. ..$ optimize : chr "minRisk"
.. .. .. .. .. .. ..$ estimator: chr "covEstimator"
.. .. .. .. .. .. ..$ tailRisk : list()
.. .. .. .. .. .. ..$ params :List of 2
.. .. .. .. .. .. .. ..$ alpha: num 0.05
.. .. .. .. .. .. .. ..$ a : num 1
.. .. .. .. .. ..# portfolio:List of 6
.. .. .. .. .. .. ..$ weights : atomic [1:14] 0.999 0 0 0 0 ...
.. .. .. .. .. .. .. ..- attr(*, "invest")= num 1
.. .. .. .. .. .. ..$ targetRisk : num 0.000155
.. .. .. .. .. .. ..$ riskFreeRate : num 0
.. .. .. .. .. .. ..$ nFrontierPoints: num 50
.. .. .. .. .. .. ..$ status : num 0
.. .. .. .. .. .. ..$ targetReturn : Named num -1
.. .. .. .. .. .. .. ..- attr(*, "names")= chr ""
.. .. .. .. .. ..# optim :List of 5
.. .. .. .. .. .. ..$ solver : chr "solveRquadprog"
.. .. .. .. .. .. ..$ objective: chr [1:3] "portfolioObjective" "portfolioReturn" "portfolioRisk"
.. .. .. .. .. .. ..$ options :List of 1
.. .. .. .. .. .. .. ..$ meq: num 2
.. .. .. .. .. .. ..$ control : list()
.. .. .. .. .. .. ..$ trace : logi FALSE
.. .. .. .. .. ..# messages :List of 2
.. .. .. .. .. .. ..$ messages: logi FALSE
.. .. .. .. .. .. ..$ note : chr ""
.. .. .. .. .. ..# ampl :List of 5
.. .. .. .. .. .. ..$ ampl : logi FALSE
.. .. .. .. .. .. ..$ project : chr "ampl"
.. .. .. .. .. .. ..$ solver : chr "ipopt"
.. .. .. .. .. .. ..$ protocol: logi FALSE
.. .. .. .. .. .. ..$ trace : logi FALSE
.. .. .. ..# constraints:Formal class 'fPFOLIOCON' [package "fPortfolio"] with 16 slots
.. .. .. .. .. ..# stringConstraints : chr "LongOnly"
.. .. .. .. .. ..# minWConstraints : Named num [1:14] 0 0 0 0 0 0 0 0 0 0 ...
.. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. ..# maxWConstraints : Named num [1:14] 1 1 1 1 1 1 1 1 1 1 ...
.. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. ..# eqsumWConstraints : num [1:2, 1:15] -1 -1 0.000277 -1 0.0011 ...
.. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. .. .. ..$ : chr [1:2] "Return" "Budget"
.. .. .. .. .. .. .. ..$ : chr [1:15] "ceq" "MNEU" "OBGVEUBR" "OBGVEUML" ...
.. .. .. .. .. ..# minsumWConstraints : logi [1, 1] NA
.. .. .. .. .. ..# maxsumWConstraints : logi [1, 1] NA
.. .. .. .. .. ..# minBConstraints : Named num [1:14] -Inf -Inf -Inf -Inf -Inf ...
.. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. ..# maxBConstraints : Named num [1:14] 1 1 1 1 1 1 1 1 1 1 ...
.. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. ..# listFConstraints : list()
.. .. .. .. .. ..# minFConstraints : num(0)
.. .. .. .. .. ..# maxFConstraints : num(0)
.. .. .. .. .. ..# minBuyinConstraints: Named num [1:14] 0 0 0 0 0 0 0 0 0 0 ...
.. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. ..# maxBuyinConstraints: Named num [1:14] 1 1 1 1 1 1 1 1 1 1 ...
.. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. ..# nCardConstraints : int 14
.. .. .. .. .. ..# minCardConstraints : Named num [1:14] 0 0 0 0 0 0 0 0 0 0 ...
.. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. ..# maxCardConstraints : Named num [1:14] 1 1 1 1 1 1 1 1 1 1 ...
.. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. ..# portfolio :Formal class 'fPFOLIOVAL' [package "fPortfolio"] with 2 slots
.. .. .. .. .. ..# portfolio:List of 6
.. .. .. .. .. .. ..$ weights : Named num [1:14] 0.999 0 0 0 0 ...
.. .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. .. ..$ covRiskBudgets: Named num [1:14] 0.999 0 0 0 0 ...
.. .. .. .. .. .. .. ..- attr(*, "names")= chr [1:14] "MNEU" "OBGVEUBR" "OBGVEUML" "OBCPEU" ...
.. .. .. .. .. .. ..$ targetReturn : Named num [1:2] 0.000279 0.000279
.. .. .. .. .. .. .. ..- attr(*, "names")= chr [1:2] "mean" "mu"
.. .. .. .. .. .. ..$ targetRisk : Named num [1:4] 1.55e-04 1.55e-04 -1.22e-05 -3.15e-05
.. .. .. .. .. .. .. ..- attr(*, "names")= chr [1:4] "Cov" "Sigma" "CVaR" "VaR"
.. .. .. .. .. .. ..$ targetAlpha : num 0.05
.. .. .. .. .. .. ..$ status : num 0
.. .. .. .. .. ..# messages : list()
.. .. .. ..# title : chr "Minimum Variance Portfolio"
.. .. .. ..# description: chr "Wed Mar 18 12:18:11 2015 by user: dallaliberaf"
.. ..$ status : num 0
..# messages : list()
It really is just standard S4, so you need to pick element by element. Here is an example which I have actually wrapped in C++ as an RInside example:
suppressMessages(library(fPortfolio))
lppData <- 100 * LPP2005.RET[, 1:6]
ewSpec <- portfolioSpec()
nAssets <- ncol(lppData)
weightsvec <- c(0.5, rep(0.1, 5))
setWeights(ewSpec) <- weightsvec
ewPf <- feasiblePortfolio(data=lppData, spec=ewSpec, constraints="LongOnly")
print(ewPf)
vec <- getCovRiskBudgets(ewPf#portfolio)
Here, you simply read the str() output wrong and omitted a layer of #portfoio (Hint: you need two) when trying to get to weights:
R> ewPf#portfolio#portfolio$weights
SBI SPI SII LMI MPI ALT
0.5 0.1 0.1 0.1 0.1 0.1
R>
which are of course the same six values I gave it earlier in the example.
Edit: Your subsequent edits proves this. You have
> str(frontier#portfolio)
Formal class 'fPFOLIOVAL' [package "fPortfolio"] with 2 slots
..# portfolio:List of 7
.. ..$ weights : num [1:49, 1:14] 0.0805 0.161 0.2415 0.322 0.4025 ..
which is precisely the frontier#portfolio#portfolio$weights I show in my code (albeit applied to the variable named the way it is in your example).

How do I extract contents from a koRpus object in R?

I'm using the tm package, and looking to get the Flesch-Kincaid scores for a document using R. I found the koRpus package has some a lot of metrics including reading-level, and started using that. However, the object returned seems to be a very complicated s4 object I don't understand how to parse.
So, I apply this to my corpus:
txt <- system.file("texts", "txt", package = "tm")
(d <- Corpus(DirSource(txt, encoding = "UTF-8"), readerControl = list(language = "lat")))
f <- function(x) tokenize(x, format="obj", lang='en')
g <- function(x) flesch.kincaid(x)
x <- foreach(i=1:5) %dopar% g(f(d[[i]]))
x is then the vector of flesch.kincaid applied to Ovid.
> x[[1]]
Flesch-Kincaid Grade Level
Parameters: default
Grade: 13.62
Age: 18.62
Text language: en
How can I get just the return values grade=13.62, and age=18.62? The str(x) is so large it's hard to parse, ie:
> str(x[[1]])
Formal class 'kRp.readability' [package "koRpus"] with 49 slots
..# hyphen :Formal class 'kRp.hyphen' [package "koRpus"] with 3 slots
.. .. ..# lang : chr "en"
.. .. ..# desc :List of 5
.. .. .. ..$ num.syll : num 196
.. .. .. ..$ syll.distrib : num [1:6, 1:4] 25 25 65 27.8 27.8 ...
.. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. ..$ : chr [1:6] "num" "cum.sum" "cum.inv" "pct" ...
.. .. .. .. .. ..$ : chr [1:4] "1" "2" "3" "4"
.. .. .. ..$ syll.uniq.distrib: num [1:6, 1:4] 15 15 61 19.7 19.7 ...
.. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. ..$ : chr [1:6] "num" "cum.sum" "cum.inv" "pct" ...
.. .. .. .. .. ..$ : chr [1:4] "1" "2" "3" "4"
.. .. .. ..$ avg.syll.word : num 2.18
.. .. .. ..$ syll.per100 : num 218
.. .. ..# hyphen:'data.frame': 90 obs. of 2 variables:
.. .. .. ..$ syll: num [1:90] 1 1 1 1 2 3 1 2 3 1 ...
.. .. .. ..$ word: chr [1:90] "Si" "quis" "in" "hoc" ...
..# param :List of 1
.. ..$ Flesch.Kincaid: Named num [1:3] 0.39 11.8 15.59
.. .. ..- attr(*, "names")= chr [1:3] "asl" "asw" "const"
..# ARI :List of 1
.. ..$ : logi NA
..# ARI.NRI :List of 1
.. ..$ : logi NA
..# ARI.simple :List of 1
.. ..$ : logi NA
..# Bormuth :List of 1
.. ..$ : logi NA
..# Coleman :List of 1
.. ..$ : logi NA
..# Coleman.Liau :List of 1
.. ..$ : logi NA
..# Dale.Chall :List of 1
.. ..$ : logi NA
..# Dale.Chall.PSK :List of 1
.. ..$ : logi NA
..# Dale.Chall.old :List of 1
.. ..$ : logi NA
..# Danielson.Bryan :List of 1
.. ..$ : logi NA
..# Dickes.Steiwer :List of 1
.. ..$ : logi NA
..# DRP :List of 1
.. ..$ : logi NA
..# ELF :List of 1
.. ..$ : logi NA
..# Flesch :List of 1
.. ..$ : logi NA
..# Flesch.PSK :List of 1
.. ..$ : logi NA
..# Flesch.de :List of 1
.. ..$ : logi NA
..# Flesch.es :List of 1
.. ..$ : logi NA
..# Flesch.fr :List of 1
.. ..$ : logi NA
..# Flesch.nl :List of 1
.. ..$ : logi NA
..# Flesch.Kincaid :List of 3
.. ..$ flavour: chr "default"
.. ..$ grade : num 13.6
.. ..$ age : num 18.6
..# Farr.Jenkins.Paterson :List of 1
.. ..$ : logi NA
..# Farr.Jenkins.Paterson.PSK:List of 1
.. ..$ : logi NA
..# FOG :List of 1
.. ..$ : logi NA
..# FOG.PSK :List of 1
.. ..$ : logi NA
..# FOG.NRI :List of 1
.. ..$ : logi NA
..# FORCAST :List of 1
.. ..$ : logi NA
..# FORCAST.RGL :List of 1
.. ..$ : logi NA
..# Fucks :List of 1
.. ..$ : logi NA
..# Harris.Jacobson :List of 1
.. ..$ : logi NA
..# Linsear.Write :List of 1
.. ..$ : logi NA
..# LIX :List of 1
.. ..$ : logi NA
..# RIX :List of 1
.. ..$ : logi NA
..# SMOG :List of 1
.. ..$ : logi NA
..# SMOG.de :List of 1
.. ..$ : logi NA
..# SMOG.C :List of 1
.. ..$ : logi NA
..# SMOG.simple :List of 1
.. ..$ : logi NA
..# Spache :List of 1
.. ..$ : logi NA
..# Spache.old :List of 1
.. ..$ : logi NA
..# Strain :List of 1
.. ..$ : logi NA
..# Traenkle.Bailer :List of 1
.. ..$ : logi NA
..# TRI :List of 1
.. ..$ : logi NA
..# Wheeler.Smith :List of 1
.. ..$ : logi NA
..# Wheeler.Smith.de :List of 1
.. ..$ : logi NA
..# Wiener.STF :List of 1
.. ..$ : logi NA
..# lang : chr "en"
..# desc :List of 26
.. ..$ sentences : int 10
.. ..$ words : int 90
.. ..$ letters : Named num [1:12] 492 0 8 9 14 18 14 9 10 6 ...
.. .. ..- attr(*, "names")= chr [1:12] "all" "l1" "l2" "l3" ...
.. ..$ all.chars : int 692
.. ..$ syllables : Named num [1:5] 196 25 32 25 8
.. .. ..- attr(*, "names")= chr [1:5] "all" "s1" "s2" "s3" ...
.. ..$ lttr.distrib : num [1:6, 1:11] 0 0 90 0 0 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:6] "num" "cum.sum" "cum.inv" "pct" ...
.. .. .. ..$ : chr [1:11] "1" "2" "3" "4" ...
.. ..$ syll.distrib : num [1:6, 1:4] 25 25 65 27.8 27.8 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:6] "num" "cum.sum" "cum.inv" "pct" ...
.. .. .. ..$ : chr [1:4] "1" "2" "3" "4"
.. ..$ syll.uniq.distrib : num [1:6, 1:4] 15 15 61 19.7 19.7 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:6] "num" "cum.sum" "cum.inv" "pct" ...
.. .. .. ..$ : chr [1:4] "1" "2" "3" "4"
.. ..$ punct : int 17
.. ..$ conjunctions : int 0
.. ..$ prepositions : int 0
.. ..$ pronouns : int 0
.. ..$ foreign : int 0
.. ..$ TTR : num 0.844
.. ..$ avg.sentc.length : num 9
.. ..$ avg.word.length : num 5.47
.. ..$ avg.syll.word : num 2.18
.. ..$ sntc.per.word : num 0.111
.. ..$ sntc.per100 : num 11.1
.. ..$ lett.per100 : num 547
.. ..$ syll.per100 : num 218
.. ..$ FOG.hard.words : NULL
.. ..$ Bormuth.NOL : NULL
.. ..$ Dale.Chall.NOL : NULL
.. ..$ Harris.Jacobson.NOL: NULL
.. ..$ Spache.NOL : NULL
..# TT.res :'data.frame': 107 obs. of 6 variables:
.. ..$ token : chr [1:107] "Si" "quis" "in" "hoc" ...
.. ..$ tag : chr [1:107] "word.kRp" "word.kRp" "word.kRp" "word.kRp" ...
.. ..$ lemma : chr [1:107] "" "" "" "" ...
.. ..$ lttr : num [1:107] 2 4 2 3 5 6 3 5 6 1 ...
.. ..$ wclass: chr [1:107] "word" "word" "word" "word" ...
.. ..$ desc : chr [1:107] "Word (kRp internal)" "Word (kRp internal)" "Word (kRp internal)" "Word (kRp internal)" ...
I'd ideally like to assign the F-K score to the meta(d) back in tm.
I'd appreciate learning either how to understand this return object and take out its values, but also, if there's another, better, faster way to get a F-K score, I'm all ears!
Similar to #Paul answer but one liner solution
sapply(lapply(x,slot,'Flesch.Kincaid'),'[',c('age','grade'))
[,1] [,2] [,3] [,4] [,5]
age 18.61778 17.62351 17.77699 18.29032 18.645
grade 13.61778 12.62351 12.77699 13.29032 13.645
Just use:
slot(x[[1]], "Flesch.Kincaid")
to get the subset of the object that contains these values. To get these in a list for each element in x, do something like:
list_fk = lapply(x, slot, "Flesch.Kincaid)
...and to get a vector with grade:
grades = sapply(list_fk, "[[", "grade")

Resources