I'm following some code from a course but using my own data. Now I get a 'subscript out of bounds' error but can't find a solution online
Tried searching online for solution, tried using a categorical variable instead of crp2 which is continuous. (TB is binary)
To check whether the relationship between crp2 and log odds of having TB is linear (an assumption of the logistic regression)
# 1. create a cross tabulation of crp2 and TB status
tb_by_crp2 <- table(crp2,Culpos)
# 2. output the frequencies of TB status by crp2
freq_table <- prop.table(tb_by_crp2, margin = 1)
# 3. calculate the odds of having TB
odds <- freq_table[, "yes"]/freq_table[, "no"]
Error in
[.default
(freq_table, , "yes") : subscript out of bounds
I wasn't expecting an error
dput(tb_by_crp2) structure(c(31L, 3L, 3L, 1L, 3L, 5L, 1L, 2L, 0L, 3L, 2L, 1L, 1L, 2L, 1L, 0L, 1L, 0L, 0L, 1L, 2L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 2L, 1L, 0L, 0L, 1L, 0L, 0L, 2L, 2L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 2L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 2L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 3L, 2L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 3L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 2L, 0L, 1L, 1L, 0L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 2L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 2L), .Dim = c(85L, 2L), .Dimnames = structure(list(crp2 = c("5", "6", "7", "8", "9", "10", "12", "13", "14", "15", "16", "18", "19", "20", "21", "22", "24", "25", "26", "27", "28", "30", "31", "33", "34", "35", "36", "37", "38", "39", "40", "42", "44", "46", "48", "51", "55", "56", "58", "60", "62", "68", "71", "72", "73", "76", "78", "81", "85", "89", "90", "91", "92", "95", "96", "97", "99", "102", "103", "104", "106", "107", "109", "114", "119", "127", "128", "131", "132", "136", "141", "148", "152", "156", "157", "159", "162", "165", "168", "173", "181", "193", "196", "199", "300" ), Culpos = c("0", "1")), .Names = c("crp2", "Culpos")), class = "table")