How do I extract contents from a koRpus object in R? - 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")

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"

R testthat_result saving in file

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"

R how to get association rules (LHS, RHS, support, confidence, lift) from recommenderlab object?

i'm current construct product recommendation using R recommenderlab, after compute AR recommender, i'm hoping to understand the association rules, but i couldn't found any why to extract the complete association rules from the recommender object.
Below is the sample dataset
m <- matrix(sample(c(0,1), 50, replace=TRUE), nrow=5, ncol=10,
dimnames=list(users=paste("u", 1:5, sep=''),
items=paste("i", 1:10, sep='')))
Convert matrix into binaryRatingMatrix
b <- as(m, "binaryRatingMatrix")
create a user-based CF recommender using training data
r <- Recommender(getData(scheme, "train"), "AR")
Looking into the AR recommender object r#model$rule_base i found "rule_base"
Formal class 'Recommender' [package "recommenderlab"] with 5 slots
..# method : chr "AR"
..# dataType: chr "binaryRatingMatrix"
..# ntrain : int 5
..# model :List of 9
.. ..$ description : chr "AR: rule base"
.. ..$ rule_base :Formal class 'rules' [package "arules"] with 4 slots
.. .. .. ..# lhs :Formal class 'itemMatrix' [package "arules"] with 3 slots
.. .. .. .. .. ..# data :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots
.. .. .. .. .. .. .. ..# i : int [1:145] 1 1 1 1 2 0 0 0 5 5 ...
.. .. .. .. .. .. .. ..# p : int [1:80] 0 1 2 3 4 5 6 7 8 9 ...
.. .. .. .. .. .. .. ..# Dim : int [1:2] 10 79
.. .. .. .. .. .. .. ..# Dimnames:List of 2
.. .. .. .. .. .. .. .. ..$ : NULL
.. .. .. .. .. .. .. .. ..$ : NULL
.. .. .. .. .. .. .. ..# factors : list()
.. .. .. .. .. ..# itemInfo :'data.frame': 10 obs. of 1 variable:
.. .. .. .. .. .. ..$ labels: chr [1:10] "i1" "i2" "i3" "i4" ...
.. .. .. .. .. ..# itemsetInfo:'data.frame': 0 obs. of 0 variables
.. .. .. ..# rhs :Formal class 'itemMatrix' [package "arules"] with 3 slots
.. .. .. .. .. ..# data :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots
.. .. .. .. .. .. .. ..# i : int [1:79] 6 4 9 3 8 4 9 3 6 3 ...
.. .. .. .. .. .. .. ..# p : int [1:80] 0 1 2 3 4 5 6 7 8 9 ...
.. .. .. .. .. .. .. ..# Dim : int [1:2] 10 79
.. .. .. .. .. .. .. ..# Dimnames:List of 2
.. .. .. .. .. .. .. .. ..$ : NULL
.. .. .. .. .. .. .. .. ..$ : NULL
.. .. .. .. .. .. .. ..# factors : list()
.. .. .. .. .. ..# itemInfo :'data.frame': 10 obs. of 1 variable:
.. .. .. .. .. .. ..$ labels: chr [1:10] "i1" "i2" "i3" "i4" ...
.. .. .. .. .. ..# itemsetInfo:'data.frame': 0 obs. of 0 variables
.. .. .. ..# quality:'data.frame': 79 obs. of 4 variables:
.. .. .. .. ..$ support : num [1:79] 0.2 0.2 0.2 0.2 0.4 0.4 0.4 0.4 0.4 0.4 ...
.. .. .. .. ..$ confidence: num [1:79] 1 1 1 1 1 1 1 1 1 1 ...
.. .. .. .. ..$ lift : num [1:79] 1.67 1.25 1.25 1.25 1.67 ...
.. .. .. .. ..$ count : num [1:79] 1 1 1 1 2 2 2 2 2 2 ...
.. .. .. ..# info :List of 4
.. .. .. .. ..$ data : symbol data
.. .. .. .. ..$ ntransactions: int 5
.. .. .. .. ..$ support : num 0.1
.. .. .. .. ..$ confidence : num 0.8
.. ..$ support : num 0.1
.. ..$ confidence : num 0.8
.. ..$ maxlen : num 3
.. ..$ sort_measure : chr "confidence"
.. ..$ sort_decreasing: logi TRUE
.. ..$ apriori_control:List of 1
.. .. ..$ verbose: logi FALSE
.. ..$ verbose : logi FALSE
..# predict :function (model, newdata, n = 10, data = NULL, type = c("topNList", "ratings", "ratingMatrix"), ...)
Question: how can i extract the association rules from recommender object as data frame?
Get association rules dataframe with columns (LHS, RHS, support, confidence, lift, count)
you can use
#Convert rules into data frame
rules3 = as(rules, "data.frame")

Extracting results from a `lmermod` object

I would like to extract some results from a lmermod object
require(lme4)
(fm1 <- lmer(Yield ~ 1|Batch, Dyestuff))
This produces
Linear mixed model fit by REML ['lmerMod']
Formula: Yield ~ 1 | Batch
Data: Dyestuff
REML criterion at convergence: 319.6543
Random effects:
Groups Name Std.Dev.
Batch (Intercept) 42.00
Residual 49.51
Number of obs: 30, groups: Batch, 6
Fixed Effects:
(Intercept)
1527
In particular, I would like to extract the standard devations of the 2 random effects, ie 42.00 and 49.51. I guess there may be a built-in method to do this, but I couldn't find it quickly so I thought I should be able to find it by using str(fm1), which produces this:
Formal class 'lmerMod' [package "lme4"] with 13 slots
..# resp :Reference class 'lmerResp' [package "lme4"] with 9 fields
.. ..$ Ptr :<externalptr>
.. ..$ mu : num [1:30] 1510 1510 1510 1510 1510 ...
.. ..$ offset : num [1:30] 0 0 0 0 0 0 0 0 0 0 ...
.. ..$ sqrtXwt: num [1:30] 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ sqrtrwt: num [1:30] 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ weights: num [1:30] 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ wtres : num [1:30] 35.1 -69.9 -69.9 10.1 70.1 ...
.. ..$ y : num [1:30] 1545 1440 1440 1520 1580 ...
.. ..$ REML : int 1
.. ..and 26 methods, of which 14 are possibly relevant:
.. .. allInfo, copy#envRefClass, initialize, initialize#lmResp,
.. .. initializePtr, initializePtr#lmResp, objective, ptr, ptr#lmResp,
.. .. setOffset, setResp, setWeights, updateMu, wrss
..# Gp : int [1:2] 0 6
..# call : language lmer(formula = Yield ~ 1 | Batch, data = Dyestuff)
..# frame :'data.frame': 30 obs. of 2 variables:
.. ..$ Yield: num [1:30] 1545 1440 1440 1520 1580 ...
.. ..$ Batch: Factor w/ 6 levels "A","B","C","D",..: 1 1 1 1 1 2 2 2 2 2 ...
.. ..- attr(*, "terms")=Classes 'terms', 'formula' length 3 Yield ~ 1 + Batch
.. .. .. ..- attr(*, "variables")= language list(Yield, Batch)
.. .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. ..$ : chr [1:2] "Yield" "Batch"
.. .. .. .. .. ..$ : chr "Batch"
.. .. .. ..- attr(*, "term.labels")= chr "Batch"
.. .. .. ..- attr(*, "order")= int 1
.. .. .. ..- attr(*, "intercept")= int 1
.. .. .. ..- attr(*, "response")= int 1
.. .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. .. ..- attr(*, "predvars")= language list(Yield, Batch)
.. .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "factor"
.. .. .. .. ..- attr(*, "names")= chr [1:2] "Yield" "Batch"
.. .. .. ..- attr(*, "predvars.fixed")= language list(Yield)
.. ..- attr(*, "formula")=Class 'formula' length 3 Yield ~ 1 | Batch
.. .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
..# flist :List of 1
.. ..$ Batch: Factor w/ 6 levels "A","B","C","D",..: 1 1 1 1 1 2 2 2 2 2 ...
.. ..- attr(*, "assign")= int 1
..# cnms :List of 1
.. ..$ Batch: chr "(Intercept)"
..# lower : num 0
..# theta : num 0.848
..# beta : num 1527
..# u : num [1:6] -20.755 0.461 33.669 -27.212 66.877 ...
..# devcomp:List of 2
.. ..$ cmp : Named num [1:10] 9.15 1.88 61495.41 9590.84 71086.25 ...
.. .. ..- attr(*, "names")= chr [1:10] "ldL2" "ldRX2" "wrss" "ussq" ...
.. ..$ dims: Named int [1:12] 30 30 1 29 1 6 1 1 0 1 ...
.. .. ..- attr(*, "names")= chr [1:12] "N" "n" "p" "nmp" ...
..# pp :Reference class 'merPredD' [package "lme4"] with 18 fields
.. ..$ Lambdat:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. .. .. ..# i : int [1:6] 0 1 2 3 4 5
.. .. .. ..# p : int [1:7] 0 1 2 3 4 5 6
.. .. .. ..# Dim : int [1:2] 6 6
.. .. .. ..# Dimnames:List of 2
.. .. .. .. ..$ : NULL
.. .. .. .. ..$ : NULL
.. .. .. ..# x : num [1:6] 0.848 0.848 0.848 0.848 0.848 ...
.. .. .. ..# factors : list()
.. ..$ LamtUt :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. .. .. ..# i : int [1:30] 0 0 0 0 0 1 1 1 1 1 ...
.. .. .. ..# p : int [1:31] 0 1 2 3 4 5 6 7 8 9 ...
.. .. .. ..# Dim : int [1:2] 6 30
.. .. .. ..# Dimnames:List of 2
.. .. .. .. ..$ : NULL
.. .. .. .. ..$ : NULL
.. .. .. ..# x : num [1:30] 0.848 0.848 0.848 0.848 0.848 ...
.. .. .. ..# factors : list()
.. ..$ Lind : int [1:6] 1 1 1 1 1 1
.. ..$ Ptr :<externalptr>
.. ..$ RZX : num [1:6, 1] 1.98 1.98 1.98 1.98 1.98 ...
.. ..$ Ut :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. .. .. ..# i : int [1:30] 0 0 0 0 0 1 1 1 1 1 ...
.. .. .. ..# p : int [1:31] 0 1 2 3 4 5 6 7 8 9 ...
.. .. .. ..# Dim : int [1:2] 6 30
.. .. .. ..# Dimnames:List of 2
.. .. .. .. ..$ : chr [1:6] "A" "B" "C" "D" ...
.. .. .. .. ..$ : NULL
.. .. .. ..# x : num [1:30] 1 1 1 1 1 1 1 1 1 1 ...
.. .. .. ..# factors : list()
.. ..$ Utr : num [1:6] 6384 6481 6634 6354 6787 ...
.. ..$ V : num [1:30, 1] 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ VtV : num [1, 1] 30
.. ..$ Vtr : num 45825
.. ..$ X : num [1:30, 1] 1 1 1 1 1 1 1 1 1 1 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:30] "1" "2" "3" "4" ...
.. .. .. ..$ : chr "(Intercept)"
.. .. ..- attr(*, "assign")= int 0
.. ..$ Xwts : num [1:30] 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ Zt :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. .. .. ..# i : int [1:30] 0 0 0 0 0 1 1 1 1 1 ...
.. .. .. ..# p : int [1:31] 0 1 2 3 4 5 6 7 8 9 ...
.. .. .. ..# Dim : int [1:2] 6 30
.. .. .. ..# Dimnames:List of 2
.. .. .. .. ..$ : chr [1:6] "A" "B" "C" "D" ...
.. .. .. .. ..$ : NULL
.. .. .. ..# x : num [1:30] 1 1 1 1 1 1 1 1 1 1 ...
.. .. .. ..# factors : list()
.. ..$ beta0 : num 0
.. ..$ delb : num 1527
.. ..$ delu : num [1:6] -20.755 0.461 33.669 -27.212 66.877 ...
.. ..$ theta : num 0.848
.. ..$ u0 : num [1:6] 0 0 0 0 0 0
.. ..and 42 methods, of which 30 are possibly relevant:
.. .. b, beta, CcNumer, copy#envRefClass, initialize, initializePtr,
.. .. installPars, L, ldL2, ldRX2, linPred, P, ptr, RX, RXdiag, RXi,
.. .. setBeta0, setDelb, setDelu, setTheta, solve, solveU, sqrL, u, unsc,
.. .. updateDecomp, updateL, updateLamtUt, updateRes, updateXwts
..# optinfo:List of 7
.. ..$ optimizer: chr "bobyqa"
.. ..$ control :List of 1
.. .. ..$ iprint: int 0
.. ..$ derivs :List of 2
.. .. ..$ gradient: num 1.61e-07
.. .. ..$ Hessian : num [1, 1] 14.1
.. ..$ conv :List of 2
.. .. ..$ opt : int 0
.. .. ..$ lme4: list()
.. ..$ feval : int 16
.. ..$ warnings : list()
.. ..$ val : num 0.848
...but this leaves me none the wiser.
lme4 supplies VarCorr for extracting variance and correlation components:
varcor <- VarCorr(fm1)
as.data.frame(varcor)
# grp var1 var2 vcov sdcor
#1 Batch (Intercept) <NA> 1764.05 42.0006
#2 Residual <NA> <NA> 2451.25 49.5101

How to extract "fixed effect design matrix" from glmer model object

The call mF#X should return fixed effect design matrix (necessary for marginal and conditional R^2 in glmm).
However it does not work.
Is even such matrix listed in model structure?
I appreciate any suggestions.
str(mF)
Formal class 'glmerMod' [package "lme4"] with 13 slots
..# resp :Reference class 'glmResp' [package "lme4"] with 11 fields
.. ..$ Ptr :<externalptr>
.. ..$ mu : num [1:480] 0.168 0.356 0.168 0.356 0.284 ...
.. ..$ offset : num [1:480] 0 0 0 0 0 0 0 0 0 0 ...
.. ..$ sqrtXwt: num [1:480] 0.373 0.479 0.373 0.479 0.451 ...
.. ..$ sqrtrwt: num [1:480] 2.68 2.09 2.68 2.09 2.22 ...
.. ..$ weights: num [1:480] 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ wtres : num [1:480] -0.449 -0.744 -0.449 1.344 -0.629 ...
.. ..$ y : num [1:480] 0 0 0 1 0 0 0 1 0 0 ...
.. ..$ eta : num [1:480] -1.603 -0.591 -1.603 -0.591 -0.927 ...
.. ..$ family :List of 11
.. .. ..$ family : chr "binomial"
.. .. ..$ link : chr "logit"
.. .. ..$ linkfun :function (mu)
.. .. ..$ linkinv :function (eta)
.. .. ..$ variance :function (mu)
.. .. ..$ dev.resids:function (y, mu, wt)
.. .. ..$ aic :function (y, n, mu, wt, dev)
.. .. ..$ mu.eta :function (eta)
.. .. ..$ validmu :function (mu)
.. .. ..$ valideta :function (eta)
.. .. ..$ simulate :function (object, nsim)
.. .. ..- attr(*, "class")= chr "family"
.. ..$ n : num [1:480] 1 1 1 1 1 1 1 1 1 1 ...
.. ..and 41 methods, of which 29 are possibly relevant:
.. .. aic, allInfo, allInfo#lmResp, copy#envRefClass, devResid, fam,
.. .. initialize, initialize#lmResp, initializePtr, Laplace, link, muEta, ptr,
.. .. ptr#lmResp, resDev, setOffset, setResp, setTheta, setWeights, sqrtWrkWt,
.. .. theta, updateMu, updateMu#lmResp, updateWts, variance, wrkResids,
.. .. wrkResp, wrss, wtWrkResp
..# Gp : int [1:3] 0 60 72
..# call : language lme4::glmer(formula = Colour ~ Treatment + Habitat + (1 | Population) + (1 | Container), data = Data, family = "binomial", control = structure(list( ...
..# frame :'data.frame': 480 obs. of 5 variables:
.. ..$ Colour : int [1:480] 0 0 0 1 0 0 0 1 0 0 ...
.. ..$ Treatment : Factor w/ 2 levels "Cont","Exp": 1 2 1 2 1 2 1 2 1 2 ...
.. ..$ Habitat : Factor w/ 2 levels "A","B": 1 1 1 1 2 2 2 2 1 1 ...
.. ..$ Population: int [1:480] 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ Container : int [1:480] 2 2 2 2 2 2 2 2 4 4 ...
.. ..- attr(*, "terms")=Classes 'terms', 'formula' length 3 Colour ~ Treatment + Habitat + (1 + Population) + (1 + Container)
.. .. .. ..- attr(*, "variables")= language list(Colour, Treatment, Habitat, Population, Container)
.. .. .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
.. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. ..$ : chr [1:5] "Colour" "Treatment" "Habitat" "Population" ...
.. .. .. .. .. ..$ : chr [1:4] "Treatment" "Habitat" "Population" "Container"
.. .. .. ..- attr(*, "term.labels")= chr [1:4] "Treatment" "Habitat" "Population" "Container"
.. .. .. ..- attr(*, "order")= int [1:4] 1 1 1 1
.. .. .. ..- attr(*, "intercept")= int 1
.. .. .. ..- attr(*, "response")= int 1
.. .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. .. ..- attr(*, "predvars")= language list(Colour, Treatment, Habitat, Population, Container)
.. .. .. ..- attr(*, "dataClasses")= Named chr [1:5] "numeric" "factor" "factor" "numeric" ...
.. .. .. .. ..- attr(*, "names")= chr [1:5] "Colour" "Treatment" "Habitat" "Population" ...
.. .. .. ..- attr(*, "predvars.fixed")= language list(Colour, Treatment, Habitat)
.. ..- attr(*, "formula")=Class 'formula' length 3 Colour ~ Treatment + Habitat + (1 | Population) + (1 | Container)
.. .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
..# flist :List of 2
.. ..$ Container : Factor w/ 60 levels "2","4","6","8",..: 1 1 1 1 1 1 1 1 2 2 ...
.. ..$ Population: Factor w/ 12 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
.. ..- attr(*, "assign")= int [1:2] 1 2
..# cnms :List of 2
.. ..$ Container : chr "(Intercept)"
.. ..$ Population: chr "(Intercept)"
..# lower : num [1:2] 0 0
..# theta : num [1:2] 0.0761 1.0537
..# beta : num [1:3] -1.251 1.012 0.676
..# u : num [1:72] -0.05 0.0254 -0.05 0.1008 -0.05 ...
..# devcomp:List of 2
.. ..$ cmp : Named num [1:11] 27.3 8.5 438.1 10.6 448.7 ...
.. .. ..- attr(*, "names")= chr [1:11] "ldL2" "ldRX2" "wrss" "ussq" ...
.. ..$ dims: Named int [1:14] 480 480 3 477 2 72 1 1 0 2 ...
.. .. ..- attr(*, "names")= chr [1:14] "N" "n" "p" "nmp" ...
..# pp :Reference class 'merPredD' [package "lme4"] with 18 fields
.. ..$ Lambdat:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. .. .. ..# i : int [1:72] 0 1 2 3 4 5 6 7 8 9 ...
.. .. .. ..# p : int [1:73] 0 1 2 3 4 5 6 7 8 9 ...
.. .. .. ..# Dim : int [1:2] 72 72
.. .. .. ..# Dimnames:List of 2
.. .. .. .. ..$ : NULL
.. .. .. .. ..$ : NULL
.. .. .. ..# x : num [1:72] 0.0761 0.0761 0.0761 0.0761 0.0761 ...
.. .. .. ..# factors : list()
.. ..$ LamtUt :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. .. .. ..# i : int [1:960] 0 60 0 60 0 60 0 60 0 60 ...
.. .. .. ..# p : int [1:481] 0 2 4 6 8 10 12 14 16 18 ...
.. .. .. ..# Dim : int [1:2] 72 480
.. .. .. ..# Dimnames:List of 2
.. .. .. .. ..$ : NULL
.. .. .. .. ..$ : NULL
.. .. .. ..# x : num [1:960] 0.0284 0.3935 0.0365 0.5046 0.0284 ...
.. .. .. ..# factors : list()
.. ..$ Lind : int [1:72] 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ Ptr :<externalptr>
.. ..$ RZX : num [1:72, 1:3] 0.124 0.125 0.124 0.125 0.124 ...
.. ..$ Ut :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. .. .. ..# i : int [1:960] 0 60 0 60 0 60 0 60 0 60 ...
.. .. .. ..# p : int [1:481] 0 2 4 6 8 10 12 14 16 18 ...
.. .. .. ..# Dim : int [1:2] 72 480
.. .. .. ..# Dimnames:List of 2
.. .. .. .. ..$ : chr [1:72] "2" "4" "6" "8" ...
.. .. .. .. ..$ : NULL
.. .. .. ..# x : num [1:960] 0.373 0.373 0.479 0.479 0.373 ...
.. .. .. ..# factors : list()
.. ..$ Utr : num [1:72] -0.094 -0.018 -0.094 0.058 -0.094 ...
.. ..$ V : num [1:480, 1:3] 0.373 0.479 0.373 0.479 0.451 ...
.. ..$ VtV : num [1:3, 1:3] 92.7 0 0 48.3 48.3 ...
.. ..$ Vtr : num [1:3] 15.738 0.879 3.455
.. ..$ X : num [1:480, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:480] "1" "2" "3" "4" ...
.. .. .. ..$ : chr [1:3] "(Intercept)" "TreatmentExp" "HabitatB"
.. .. ..- attr(*, "assign")= int [1:3] 0 1 2
.. .. ..- attr(*, "contrasts")=List of 2
.. .. .. ..$ Treatment: chr "contr.treatment"
.. .. .. ..$ Habitat : chr "contr.treatment"
.. ..$ Xwts : num [1:480] 0.373 0.479 0.373 0.479 0.451 ...
.. ..$ Zt :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
.. .. .. ..# i : int [1:960] 0 60 0 60 0 60 0 60 0 60 ...
.. .. .. ..# p : int [1:481] 0 2 4 6 8 10 12 14 16 18 ...
.. .. .. ..# Dim : int [1:2] 72 480
.. .. .. ..# Dimnames:List of 2
.. .. .. .. ..$ : chr [1:72] "2" "4" "6" "8" ...
.. .. .. .. ..$ : NULL
.. .. .. ..# x : num [1:960] 1 1 1 1 1 1 1 1 1 1 ...
.. .. .. ..# factors : list()
.. ..$ beta0 : num [1:3] 0 0 0
.. ..$ delb : num [1:3] 0 0 0
.. ..$ delu : num [1:72] -0.05 0.0254 -0.05 0.1008 -0.05 ...
.. ..$ theta : num [1:2] 0.0761 1.0537
.. ..$ u0 : num [1:72] 0 0 0 0 0 0 0 0 0 0 ...
.. ..and 42 methods, of which 30 are possibly relevant:
.. .. b, beta, CcNumer, copy#envRefClass, initialize, initializePtr,
.. .. installPars, L, ldL2, ldRX2, linPred, P, ptr, RX, RXdiag, RXi, setBeta0,
.. .. setDelb, setDelu, setTheta, solve, solveU, sqrL, u, unsc, updateDecomp,
.. .. updateL, updateLamtUt, updateRes, updateXwts
..# optinfo:List of 7
.. ..$ optimizer: chr "Nelder_Mead"
.. ..$ control :List of 3
.. .. ..$ xst : num [1:5] 0.02 0.02 0.0721 0.0421 0.0418
.. .. ..$ xt : num [1:5] 1.00e-05 1.00e-05 3.60e-05 2.11e-05 2.09e-05
.. .. ..$ verbose: int 0
.. ..$ derivs :List of 2
.. .. ..$ gradient: num [1:5] 4.64e-05 9.60e-05 -6.73e-05 -6.46e-05 -1.26e-04
.. .. ..$ Hessian : num [1:5, 1:5] 2.644 -0.247 0.162 -1.023 -0.625 ...
.. ..$ conv :List of 2
.. .. ..$ opt : num 0
.. .. ..$ lme4: list()
.. ..$ feval : num 232
.. ..$ warnings : list()
.. ..$ val : num [1:5] 0.0761 1.0537 -1.2511 1.0118 0.676
You can use the function model.matrix to obtain the fixed-effects design matrix:
model.matrix(fit)
where fit is the object returned by glmer.

Resources