0

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")

Tom Boyles
  • 13
  • 3

1 Answers1

0

With the data you're providing the column names of freq_table aren't "yes" and "no" but 0 and 1.

So, if you change that to the column index it works perfectly fine:



odds <- freq_table[, 1]/freq_table[, 2]

> head(odds, n = 10)
   5    6    7    8    9   10   12   13   14   15 
15.5  3.0  Inf  Inf  Inf  Inf  Inf  Inf  0.0  3.0 

Hope that solves your problem.

Humpelstielzchen
  • 6,126
  • 3
  • 14
  • 34