Read a three way ouput table in R - r

I hope you can help me.
I would like to know how to read an output in R.
So I have three binary variables Q3A1, Q3A2, Q3A3, each line corresponds to the answer of a subject
Q3A1
Q3A2
Q3A3
0
1
0
1
0
1
0
1
0
1
0
1
I wanted to have the number of people for each possible answer, so I wrote the command
table(dataQ$Q3A1,dataQ$Q3A2,dataQ$Q3A3)
and I got this as an output in R console
, , = 0
| | 0 |1 |
| 0 | 90 |16|
| 1 | 80 |61|
, , = 1
| | 0 |1 |
| 0 | 19 |4 |
| 1 | 52 |13|
but I don't know how to read it, which rows and columns corresponds to which variable ?
Thanks in advance for your help !

Related

Data preparation before running exact logistic (elrm in R)

I started out using Firth's logistic (logistf) to deal with my small sample size (n=80), but wanted to try out exact logistic regression using the elrm package. However, I'm having trouble figuring out how to create the "collapsed" data required for elrm to run. I have a csv that I import into R as a dataframe that has the following variables/columns. Here is some example data (real data has a few more columns and 80 rows):
+------------+-----------+-----+--------+----------------+
| patien_num | asymmetry | age | female | field_strength |
+------------+-----------+-----+--------+----------------+
| 1 | 1 | 25 | 1 | 1.5 |
| 2 | 0 | 50 | 0 | 3 |
| 3 | 0 | 75 | 1 | 1.5 |
| 4 | 0 | 33 | 1 | 3 |
| 5 | 0 | 66 | 1 | 3 |
| 6 | 0 | 99 | 0 | 3 |
| 7 | 1 | 20 | 0 | 1.5 |
| 8 | 1 | 40 | 1 | 3 |
| 9 | 0 | 60 | 1 | 3 |
| 10 | 0 | 80 | 0 | 1.5 |
+------------+-----------+-----+--------+----------------+
Basically my data is one line per patient (not a frequency table). I'm trying to run a regression with asymmetry as the dependent variable and age (continuous), female (binary), and field_strength (factor) as independent variables. I'm trying to understand how to collapse this into the appropriate format so I can get that "ntrials" part required for the elrm formula.
I've looked at https://stats.idre.ucla.edu/r/dae/exact-logistic-regression/ but they start with data in a different format than mine, and having trouble. Any help appreciated!

Subsetting a table in R

In R, I've created a 3-dimensional table from a dataset. The three variables are all factors and are labelled H, O, and S. This is the code I used to simply create the table:
attach(df)
test <- table(H, O, S)
Outputting the flattened table produces this table below. The two values of S were split up, so these are labelled S1 and S2:
ftable(test)
+-----------+-----------+-----+-----+
| H | O | S1 | S2 |
+-----------+-----------+-----+-----+
| Isolation | Dead | 2 | 15 |
| | Sick | 64 | 20 |
| | Recovered | 153 | 379 |
| ICU | Dead | 0 | 15 |
| | Sick | 0 | 2 |
| | Recovered | 1 | 9 |
| Other | Dead | 7 | 133 |
| | Sick | 4 | 20 |
| | Recovered | 17 | 261 |
+-----------+-----------+-----+-----+
The goal is to use this table object, subset it, and produce a second table. Essentially, I want only "Isolation" and "ICU" from H, "Sick" and "Recovered" from O, and only S1, so it basically becomes the 2-dimensional table below:
+-----------+------+-----------+
| | Sick | Recovered |
+-----------+------+-----------+
| Isolation | 64 | 153 |
| ICU | 0 | 1 |
+-----------+------+-----------+
S = S1
I know I could first subset the dataframe and then create the new table, but the goal is to subset the table object itself. I'm not sure how to retrieve certain values from each dimension and produce the reduced table.
Edit: ANSWER
I now found a much simpler method. All I needed to do was reference the specific columns in their respective directions. So a much simpler solution is below:
> test[1:2,2:3,1]
O
H Sick Healed
Isolation 64 153
ICU 0 1
Subset the data before running table, example:
ftable(table(mtcars[, c("cyl", "gear", "vs")]))
# vs 0 1
# cyl gear
# 4 3 0 1
# 4 0 8
# 5 1 1
# 6 3 0 2
# 4 2 2
# 5 1 0
# 8 3 12 0
# 4 0 0
# 5 2 0
# subset then run table
ftable(table(mtcars[ mtcars$gear == 4, c("cyl", "gear", "vs")]))
# vs 0 1
# cyl gear
# 4 4 0 8
# 6 4 2 2

(AVB)&(AV~B) is logically equivalent to ~~A, it that true or false?

(AVB)&(AV~B) is logically equivalent to ~~A?
It kind of confused me since they are in different dimension.
Starting from:
(A∨B)∧(A∨¬B)
One can first apply one of De Morgan's laws to the AND:
¬(¬(A∨B)∨¬(A∨¬B))
Next, one of De Morgan's laws can be applied to each side:
¬((¬A∧¬B)∨(¬A∧B))
By applying distributivity of AND over OR in reverse, ¬A can be extracted:
¬(¬A∧(¬B∨B))
By complementation, ¬B∨B is 1:
¬(¬A∧1)
By identity for AND:
¬¬A
Applying double negation:
A
This goes through your target ¬¬A without going through simple A. It's much simpler not to do that.
Starting again from:
(A∨B)∧(A∨¬B)
By applying distributivity of OR over AND in reverse, A can be extracted:
A∨(B∧¬B)
By complementation, B∨¬B is 1:
A∧1
By identity for AND:
A
Applying double negation in reverse:
¬¬A
Since you mention doing it by truth table, here's my go at showing that they're equivalent that way:
+---+---+----+----+-------+--------+--------------+-----+
| A | B | ¬A | ¬B | (A∨B) | (A∨¬B) | (A∨B)∧(A∨¬B) | ¬¬A |
+---+---+----+----+-------+--------+--------------+-----+
| 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 |
| 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 |
| 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 |
+---+---+----+----+-------+--------+--------------+-----+

Finding the starting location of specific consecutive values in every row

I am kind of new to R and am still learning how to use all the functions. I'm stuck with this problem on my project, so would definitely appreciate any help!
I have the spending behavior data of customer 1, 2, 3, and 4 from Jan to Dec. Every row is a unique customer, and every column is the customer's spending activity of that month (1 is active, 0 is inactive).
+------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| Name | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
+------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| Customer 1 | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 0 | 1 |
| Customer 2 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 0 | 1 |
| Customer 3 | 1 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 |
| Customer 4 | 1 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 1 | 0 |
+------------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
I am trying to find the "last" occurrence of 3 consecutive zero's in every row, if any, and locate the starting point of them. This would help me identify when the customer goes into "idle" or "hibernation" status.
The expected results would be:
Jun (or 6 as column index) for Customer 1
Sep (or 9 as column index) for Customer 2
Oct (or 10 as column index) for Customer 3, since the last 3-month occurrence begins at Oct instead of Sep
NA for Customer 4, since there's no such an occurrence
After going through some similar questions on stackoverflow, I figured using rle and apply might be the right approach, but I have been struggling with how to write this into actual code. Sincerely appreciate any idea!
I would melt the dataframe so there are 3 columns:
Name, Month, Spending Behaviour.
I would then do a rollmean from the zoo package with a left align, subset for instances where the rollmean is 0 and pick the last observation for each customer.

How to view a table of tables in Qt?

I using Qt library. And I need to show a table of tables, and sort data in each sub-table.
For examlple something like this (2x2 table of 3x3 tables)
sub 1 | sub 2
-------------------------
| i | 0 | 0 | c | 0 | 0 | s
------------------------- u
| j | 0 | 0 | d | 0 | 0 | b
------------------------- 1
| k | 0 | 0 | e | 0 | 0 |
-------------------------------
| a | 0 | 0 | c | 0 | 0 |
------------------------- s
| b | 0 | 0 | d | 0 | 0 | u
------------------------- b
| c | 0 | 0 | e | 0 | 0 | 2
-------------------------
Any solutions are welcome.
ps. My idea is to implement a custom model with 2d array of models.
You can nest QTableView instances through use of the QAbstractItemView::setIndexWidget method. The data structure you use to maintain the QAbstractTableModel instances is inconsequential as long as each QTableView is assigned an appropriate model instance.

Resources