Factor analysis on ordinal data - r

I'm trying to do some factor analysis on some ordinal data I have, i.e. Likert scale questionnaires going from 0 to 5.
I have been reading about how this should be approached since it is not continuous data. I then found the following paper:
http://pareonline.net/pdf/v19n5.pdf
And from that, even though he uses a different program than R, it seems that when dealing with ordinal data, it is a must to use the polychoric correlation, and he also suggests minimum rank factor analysis for the factor method. I'm a bit unsure if the Promin rotation he suggests is available in R ?
Anyway, when trying to perform this analysis in R I try to use the following code from the psych package:
fa(data, cor = "poly", fm = "minrank")
However, when doing so I get the error (even if I put in the term nfactors = <some number>:
Error in glb.algebraic(S) : 'Cov' is not symmetric
I also get a lot of warnings saying In log(P) : NaNs produced.
I have no idea what is wrong here, and how to fix it ? My data set (the one I'm using now) is just a 219 x 19 matrix with values going from 0 to 5, and no blank cells at all.
Am I doing something wrong here, or...?
EDIT:
The header of the data:
# A tibble: 6 x 19
`Pt id` Q1 Q2 Q3 Q4 Q7 Q8 Q10 Q12 Q15 Q17 Q18 Q20 Q21 Q22 Q23 Q24
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 304 5 4 5 0 3 5 0 4 1 4 4 4 4 4 4 4
2 474 5 1 5 0 3 5 0 4 1 4 0 0 0 4 4 0
3 509 5 5 4 5 3 5 0 4 1 0 0 4 4 4 4 4
4 232 5 1 4 0 3 1 0 4 0 0 0 0 0 3 3 0
5 254 5 2 3 0 3 4 0 2 1 0 0 0 1 2 2 0
6 372 5 2 3 0 3 2 0 2 0 0 1 0 0 4 4 0
# ... with 2 more variables: Q25 <dbl>, Q26 <dbl>
And all the data from dput:
structure(list(`Pt id` = c(304, 474, 509, 232, 254, 372, 388,
493, 283, 296, 305, 468, 257, 258, 362, 353, 471, 210, 213, 234,
314, 319, 332, 336, 339, 350, 357, 359, 363, 365, 370, 385, 411,
417, 436, 455, 461, 462, 495, 521, 317, 211, 204, 299, 373, 405,
423, 328, 349, 497, 202, 207, 215, 239, 245, 250, 293, 294, 297,
303, 310, 326, 341, 369, 379, 416, 430, 433, 442, 448, 456, 477,
486, 503, 520, 337, 382, 407, 498, 249, 262, 266, 438, 510, 320,
309, 267, 292, 335, 206, 216, 237, 251, 269, 324, 343, 355, 360,
375, 395, 399, 403, 404, 412, 424, 440, 457, 500, 288, 244, 279,
311, 342, 389, 447, 508, 291, 318, 439, 209, 224, 229, 275, 277,
330, 368, 390, 394, 406, 459, 469, 476, 479, 523, 201, 203, 205,
218, 219, 222, 225, 226, 227, 231, 236, 238, 242, 247, 248, 260,
261, 264, 271, 272, 278, 285, 298, 300, 301, 306, 323, 327, 329,
331, 333, 340, 345, 347, 351, 356, 358, 361, 364, 366, 367, 371,
376, 377, 378, 384, 397, 398, 400, 401, 402, 408, 413, 414, 419,
420, 425, 431, 437, 441, 444, 450, 451, 452, 453, 458, 460, 463,
466, 470, 483, 487, 492, 502, 505, 506, 511, 515, 517), Q1 = c(5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 5, 3, 4, 5, 5, 4, 0, 5, 5, 0, 0,
5, 5, 0, 3, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 0, 0, 0, 0, 5, 3, 0,
4, 4, 0, 3, 1, 4, 3, 0, 3, 5, 5, 0, 5, 2, 0, 0, 3, 0, 3, 0, 0,
4, 5, 4, 5, 5, 1, 0, 0, 0, 5, 0, 0, 2, 5, 4, 1, 1, 0, 3, 5, 1,
5, 0, 3, 1, 5, 3, 0, 1, 0, 0, 0, 0, 4, 1, 1, 0, 0, 1, 5, 0, 3,
0, 3, 5, 5, 0, 1, 4, 1, 0, 1, 4, 3, 0, 1, 0, 0, 2, 2, 4, 0, 1,
3, 3, 0, 4, 0, 0, 1, 0, 2, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 3,
2, 3, 0, 0, 5, 3, 0, 3, 0, 3, 0, 2, 0, 0, 0, 0, 0, 0, 3, 0, 0,
5, 4, 3, 0, 3, 3, 0,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, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1, 1, 0, 2, 0, 0, 1,
0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 1, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 3, 0, 0, 0, 0), Q26 = c(3, 0, 2, 0, 2, 0,
1, 1, 0, 1, 1, 0, 3, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 1,
2, 1, 1, 0, 2, 2, 0, 0, 0, 4, 0, 4, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 2, 0, 1, 0,
1, 0, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0,
0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 1, 0, 2, 4, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0,
0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 1, 0, 1, 0, 0, 2, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("Pt id",
"Q1", "Q2", "Q3", "Q4", "Q7", "Q8", "Q10", "Q12", "Q15", "Q17",
"Q18", "Q20", "Q21", "Q22", "Q23", "Q24", "Q25", "Q26"), row.names = c(NA,
-213L), class = c("tbl_df", "tbl", "data.frame"))
So basically some questionnaires/Likert scale values coupled to a patient ID.

Related

Plotting a heat map of binary and continious values

I have this matrix of pathway analysis. Genes in each pathway gets 1 if not 0. I also have fold change for genes. I tried to plot this but fold change being confused with absence and pretense of genes like :
I have tried
heatmap(m)
dput(m)
structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1,
1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0,
1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0,
1, 1, 1, 1, -0.903826171835163, -0.247705285294349, -0.401239527828149,
-1.20189443333319, -0.758305343685411, -1.10106389326754, -0.585488441571586,
-0.610209849183418, -1.28841166063443, -1.2443689942311, -0.44380661806954,
-0.393767679922636, -0.9865911233588, -1.16638985537113, -0.846196153682942,
-1.08069734237831, -0.585488441571586, -0.501442757103725, -1.09515274600357,
-1.28841166063443, -0.758305343685411, -0.358909656766957, -0.435604213681894,
-0.233037798755743, -0.401239527828149, -1.28841166063443, -0.563163557390339,
-1.08069734237831, -0.247705285294349, -0.903826171835163, -0.9865911233588,
0.377359646511066, 0.453386357728066, 1.70862112102298, 0.532342427101257,
1.61935728655752, 1.15211775940032, 0.265720938549918, 0.420856476956162,
0.377359646511066, 0.350765937539292, 0.276646264257092), .Dim = c(42L,
8L), .Dimnames = list(c("CCL2", "CCL4", "CD40", "CLCF1", "CSF3",
"CXCL5", "CXCL6", "CXCL8", "IL1B", "IL6", "IL6R", "LTB", "OSM",
"TNFRSF1B", "TNFSF10", "FOS", "FOSL1", "MMP3", "PTGS2", "TNFAIP3",
"BCL2A1", "BIRC3", "CFLAR", "DDX58", "GADD45B", "TIRAP", "EDN1",
"SOCS3", "IGF2", "JAK1", "NR4A1", "CACNA1D", "CALML5", "CALML6",
"CCNA2", "CCND2", "CDK6", "MAPK1", "RBL1", "CDC6", "CDKN2C",
"SMC3"), c("Cytokine-cytokine receptor interaction", "IL-17 signaling",
"NF-kappa B signaling", "TNF signaling", "PI3K-Akt signaling",
"Cellular senescence", "Cell cycle", "Log2FC")))
> head(m)
Cytokine-cytokine receptor interaction IL-17 signaling
CCL2 1 1
CCL4 1 0
CD40 1 0
CLCF1 1 0
CSF3 1 1
CXCL5 1 1
NF-kappa B signaling TNF signaling PI3K-Akt signaling
CCL2 0 1 0
CCL4 1 0 0
CD40 1 0 0
CLCF1 0 0 0
CSF3 0 0 1
CXCL5 0 1 0
Cellular senescence Cell cycle Log2FC
CCL2 0 0 -0.9038262
CCL4 0 0 -0.2477053
CD40 0 0 -0.4012395
CLCF1 0 0 -1.2018944
CSF3 0 0 -0.7583053
CXCL5 0 0 -1.1010639
>
Can you help to solve this?
heatmap(as.matrix(m[,1:7])) should do the trick.

How do I compute the average per row of multiple numeric columns

I have 8 age categories as 8 separate columns. Each column has a value between 1 and 3. I want to compute a new column that holds the average age per row.
This is my data:
structure(list(`2.5` = c(0, 0, 0, 1, 1, 2, 1, 2, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0,
0, 2, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 2, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 2, 0, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 2, 2,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 2, 0, 2, 0, 1, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0,
0, 1, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0,
0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 3,
0, 0, 1), `9` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0,
1, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,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column names. Then, with rowSums calculate average ages. (Supposed your data frame is called d.)
tmp <- do.call(cbind, lapply(seq(d), function(x) d[x] * as.numeric(colnames(d)[x])))
d$mu <- rowSums(tmp) / rowSums(d)
head(d)
# 2.5 9 15.5 21.5 29.5 42 57 72 mu
# 1 0 0 0 0 0 0 0 2 72.00000
# 2 0 0 0 0 1 0 1 0 43.25000
# 3 0 0 0 0 2 0 0 0 29.50000
# 4 1 0 0 0 0 2 0 0 28.83333
# 5 1 0 0 0 1 1 0 1 36.50000
# 6 2 0 0 0 0 2 0 0 22.25000
apply is a useful option, where 1 tells it to compute by row. It also seems to play nicely with data.tables:
df$means <- apply(df, 1, function(r) sum(r * as.double(names(df))) / sum(r))
#### OUTPUT ####
2.5 9 15.5 21.5 29.5 42 57 72 means
1: 0 0 0 0 0 0 0 2 72.00000
2: 0 0 0 0 1 0 1 0 43.25000
3: 0 0 0 0 2 0 0 0 29.50000
4: 1 0 0 0 0 2 0 0 28.83333
5: 1 0 0 0 1 1 0 1 36.50000
---
200: 0 0 0 0 0 0 1 1 64.50000
201: 3 0 0 0 0 2 0 0 18.30000
202: 0 0 1 0 0 1 1 0 38.16667
203: 0 0 0 0 1 1 0 0 35.75000
204: 1 1 0 0 0 2 0 0 23.87500
Here is a base R one-liner where we multiply values in the columns of dataframe by its names, calculate the sum of column values and divide by its rowSums.
df$result <- colSums(t(df) * as.numeric(names(df)))/rowSums(df)
head(df)
# 2.5 9 15.5 21.5 29.5 42 57 72 result
#1 0 0 0 0 0 0 0 2 72.00000
#2 0 0 0 0 1 0 1 0 43.25000
#3 0 0 0 0 2 0 0 0 29.50000
#4 1 0 0 0 0 2 0 0 28.83333
#5 1 0 0 0 1 1 0 1 36.50000
#6 2 0 0 0 0 2 0 0 22.25000

colnames and rownames with pairwise distance matrix outputs

EDIT:
I am trying to collect these values/Cols/Rows
** The numbers have changed slightly.
I am trying to extract the pairwise result of the following matrix.
ID1_2001 ID2_2001 ID3_2001 ID1_2000 ID2_2000
ID2_2001 0.96747537
ID3_2001 0.96850817 0.67983338
ID1_2000 0.11324889 0.97507292 0.97586446
ID2_2000 1.00000000 0.75336751 0.83321843 1.00000000
ID3_2000 1.00000000 0.76556229 0.81577353 1.00000000 0.05728332
That is the values of 0.1132489, 0.7533675, 0.8157735.
Thanks to another user on this site I know of the following function proxy::dist(m[1:3,], m[4:6,], pairwise=TRUE, method="cosine") which gives me just the following results 0.1132489 0.7533675 0.8157735.
However I would also like the column and row names from where the result comes from. So 0.1132489 would be assigned to ID1_2000_ID1_2001, and 0.7533675 assigned to ID2_2000_ID2_2001, and finally 0.81577353 assigned to ID3_2000_ID3_2001. However I cannot put this distance matrix into a data frame to Access/extract row_names and colnames.
It would be most optimal to run just the following proxy::dist(m[1:3,], m[4:6,], pairwise=TRUE, method="cosine") and obtain the pairwise results along with their colnames and rownames (saving on computational time).
How can I replace the m[1:3] with "groups", i.e. take 2001 group and then take 2000 group. Since I hope to scale this up to more years/IDs and I cannot count the rows 1:3 and 4:6 for all years/IDs.
library(tidyr)
x <- m %>%
data.frame() %>%
tibble::rownames_to_column("rownames") %>%
separate(rownames, c("id", "year"), "_")
Other:
dist.matrix = proxy::dist(m, pairwise = TRUE, method = "cosine")
proxy::dist(m[1:3,], m[4:6,], pairwise=TRUE, method="cosine")
Data:
data <- structure(c(0.96747537487273, 0.968508167135111, 0.113248890901578,
1, 1, 0.67983337671352, 0.97507292188601, 0.753367507803825,
0.765562291938692, 0.975864460398726, 0.833218430412641, 0.815773525411265,
1, 1, 0.0572833227621783), Size = 6L, Labels = c("ID1_2001",
"ID2_2001", "ID3_2001", "ID1_2000", "ID2_2000", "ID3_2000"), class = "dist", Diag = FALSE, Upper = FALSE, method = "cosine", call = proxy::dist(x = m,
method = "cosine", pairwise = TRUE))
Data 2 (m)
m <- structure(c(0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0,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 1, 0, 0, 0, 1, 0, 0, 1,
0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0,
0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 0, 0, 2, 0, 0, 0,
0, 0, 0, 1, 1), .Dim = c(6L, 196L), .Dimnames = list(Docs = c("ID1_2001",
"ID2_2001", "ID3_2001", "ID1_2000", "ID2_2000", "ID3_2000"),
Terms = c("-field", "(22-yard)", "(doubles).", "(either",
"(known", "(singles)", "(specifically", "20-metre", "able",
"across", "activity", "adjudicated", "aided", "although",
"american", "appears", "appears.", "around", "association",
"australian", "badminton", "bails", "bails,", "balanced",
"ball", "bat--ball", "bat,", "batting", "beach", "bowled",
"bowled,", "bowling", "called", "can", "canadian", "casual",
"catching", "centre", "certain", "codes", "common", "commonly",
"communicate", "comprising", "context", "cord", "countries",
"countries);", "court", "court.", "covered", "cricket", "degrees",
"degrees,", "different", "dislodges", "dismiss", "dismissal",
"dismissed,", "doubles", "each", "either", "eleven", "end,",
"ends", "family", "felt", "field", "fielding", "football",
"football);", "football.[1][2]", "football;", "football12",
"form", "formal", "forms", "gaelic", "gain", "game", "games",
"goal", "goal.", "gridiron", "ground.", "half", "hit", "hits",
"hollow", "include", "individually", "indoor", "information.",
"innings", "international", "involve", "involve,", "kicking",
"known", "landing", "larger", "league", "maneuver", "match",
"match's", "matches.", "may", "means", "net", "object", "often",
"one", "opponent", "opponent's", "opposing", "opposite",
"outdoor", "per", "pitch", "places", "play", "played", "player",
"players", "point,", "points", "popular", "prevent", "racket",
"racquet", "racquets", "record", "rectangular", "refer",
"referee", "regional", "return", "return.", "roles.", "rubber",
"rugby", "rules", "runs", "score", "scored", "scorers", "scores",
"shuttlecock", "side", "sides", "single", "singles", "soccer",
"specifically", "sport", "sports", "statistical", "strike",
"striking", "strung", "stumps", "stumps.", "swap", "team",
"teams", "ten", "tennis", "the", "these", "they", "third",
"three", "tries", "two", "umpire", "umpires,", "unable",
"understood", "union", "union);", "unqualified", "unqualified,",
"uses", "using", "valid", "variations", "varying", "way",
"when", "whichever", "wicket", "will", "will.", "within",
"word", "yard")))
EDIT:
I found this workaround to put into a data frame. Not sure how efficient it will be on the large matrix
x <- data.matrix(dist.matrix)
x <- as.data.frame(x)
EDIT2:
> data.frame(rownames(dist.matrix), colnames(dist.matrix), as.vector(dist.matrix))
rownames.dist.matrix. colnames.dist.matrix. as.vector.dist.matrix.
1 ID1_2001 ID2_2001 0.97192896
2 ID1_2001 ID2_2001 0.97288923
3 ID1_2001 ID2_2001 0.01505221
4 ID1_2001 ID2_2001 1.00000000
5 ID1_2001 ID2_2001 1.00000000
6 ID1_2001 ID2_2001 0.69527190
7 ID1_2001 ID2_2001 0.97565046
8 ID1_2001 ID2_2001 0.75908178
9 ID1_2001 ID2_2001 0.77099402
10 ID1_2001 ID2_2001 0.97648342
11 ID1_2001 ID2_2001 0.77840308
12 ID1_2001 ID2_2001 0.76921180
13 ID1_2001 ID2_2001 1.00000000
14 ID1_2001 ID2_2001 1.00000000
15 ID1_2001 ID2_2001 0.05728332
EDIT 3:
I run the following;
dist.matrix = as.matrix(dist.matrix)
df <- data.frame(row = rownames(dist.matrix),
col = colnames(dist.matrix),
value = as.vector(dist.matrix))
Which gives me the following output:
row col value
1 ID1_2001 ID1_2001 0.00000000
2 ID2_2001 ID2_2001 0.97192896
3 ID3_2001 ID3_2001 0.97288923
4 ID1_2000 ID1_2000 0.01505221
5 ID2_2000 ID2_2000 1.00000000
6 ID3_2000 ID3_2000 1.00000000
7 ID1_2001 ID1_2001 0.97192896
8 ID2_2001 ID2_2001 0.00000000
9 ID3_2001 ID3_2001 0.69527190
10 ID1_2000 ID1_2000 0.97565046
11 ID2_2000 ID2_2000 0.75908178
12 ID3_2000 ID3_2000 0.77099402
13 ID1_2001 ID1_2001 0.97288923
14 ID2_2001 ID2_2001 0.69527190
15 ID3_2001 ID3_2001 0.00000000
16 ID1_2000 ID1_2000 0.97648342
17 ID2_2000 ID2_2000 0.77840308
18 ID3_2000 ID3_2000 0.76921180
19 ID1_2001 ID1_2001 0.01505221
20 ID2_2001 ID2_2001 0.97565046
21 ID3_2001 ID3_2001 0.97648342
22 ID1_2000 ID1_2000 0.00000000
23 ID2_2000 ID2_2000 1.00000000
24 ID3_2000 ID3_2000 1.00000000
25 ID1_2001 ID1_2001 1.00000000
26 ID2_2001 ID2_2001 0.75908178
27 ID3_2001 ID3_2001 0.77840308
28 ID1_2000 ID1_2000 1.00000000
29 ID2_2000 ID2_2000 0.00000000
30 ID3_2000 ID3_2000 0.05728332
31 ID1_2001 ID1_2001 1.00000000
32 ID2_2001 ID2_2001 0.77099402
33 ID3_2001 ID3_2001 0.76921180
34 ID1_2000 ID1_2000 1.00000000
35 ID2_2000 ID2_2000 0.05728332
36 ID3_2000 ID3_2000 0.00000000
EDIT 4:
x <- data.matrix(dist.matrix)
x <- as.data.frame(x)
library(tibble)
library(tidyr)
y <- x %>%
rownames_to_column("row") %>%
separate(row, c("id_row", "year_row"), "_")
z <- melt(y)
z
w <- z %>%
separate(variable, c("id_col", "year_col"), "_")
w
Which seems to give
> head(w)
id_row year_row id_col year_col value
1 ID1 2001 ID1 2001 0.00000000
2 ID2 2001 ID1 2001 0.97192896
3 ID3 2001 ID1 2001 0.97288923
4 ID1 2000 ID1 2001 0.01505221
5 ID2 2000 ID1 2001 1.00000000
6 ID3 2000 ID1 2001 1.00000000
Just stick the rownames and colnames in a dataframe alongside the data itself. "Unraveling" the matrix as a vector (and vector recycling for the names) will take care of the rest:
# example data
mat <- matrix(1:100, 10, 10)
rownames(mat) <- paste0("row",1:10)
colnames(mat) <- paste0("col",1:10)
# what you want
df <- data.frame(row = rownames(mat),
col = colnames(mat),
value = as.vector(mat) )
# take a look at the result
head(df)
# row col value
# row1 col1 1
# row2 col2 2
# row3 col3 3
# row4 col4 4
# row5 col5 5
# row6 col6 6

Counts by group and calculation/display of tables

I have information from science papers, where there are binary data on funding source (government (GVT) - yes/no), and categorical data on lead author nationality (LEAD).
This code counts the number of GTV funded papers per lead nation:
gvt.funding=aggregate(data.frame(count=sysrev$GVT),
list(value=sysrev$GVT),length)
gvt.not=format(round(((gvt_funding[1,2]/(gvt_funding[1,2]+
gvt_funding[2,2]))*100),1),nsmall=1)
gvt.yes=format(round(((gvt_funding[2,2]/(gvt_funding[1,2]+
gvt_funding[2,2]))*100),1),nsmall=1)
This code produces the following table:
gvt.not gvt.yes gvt.not(%) gvt.yes(%)
CAN 39 143 21.42857 78.571429
DEU 7 7 50.00000 50.000000
DNK 9 35 20.45455 79.545455
GBR 10 5 66.66667 33.333333
JPN 2 8 20.00000 80.000000
NOR 20 49 28.98551 71.014493
RUS 13 1 92.85714 7.142857
USA 84 104 44.68085 55.319149
TOTAL 214 373 36.45656 63.543441
I need a table aggregated pr year looking like this:
YEAR CAN-GVTyes CAN% DEU-GVTyes DEU% USA-GVTyes USA% etc.
2015 .... .. .... .. .... ..
2014 .... .. .... .. .... ..
2013 .... .. .... .. .... ..
etc
Appreciate any help. Below is an DPUT excerpt of data:
structure(list(YR = c(2015, 2015, 2015, 2015, 2015, 2015, 2015,
2015, 2015, 2015, 2015, 2015, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2013, 2013, 2013, 2013, 2013, 2013,
2013, 2013, 2013), NRAUTH = c(6, 7, 3, 22, 10, 4, 23, 4, 4, 11,
9, 6, 7, 9, 4, 6, 1, 3, 5, 4, 7, 5, 3, 2, 2, 4, 7, 4, 26, 6,
2, 4,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0, 0, 0, 0), PA = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), PEFF = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("YR",
"NRAUTH", "LEAD", "CAN", "DNK", "GRL", "USA", "NOR", "RUS", "NATX",
"ALL", "AB", "BB", "BS", "CS", "DS", "EG", "FB", "GB", "KB",
"KS", "LS", "LP", "MC", "NB", "NW", "SB", "SH", "VM", "WH", "GVT",
"NGO", "COM", "ACA", "FUNDX", "FUNDNN", "POPSTAT", "POPABU",
"POPTR", "BOUND", "HARV", "CC", "HAB", "HABP", "POLL", "SHIP",
"TOUR", "BEH", "REPEC", "ZOO", "PHYS", "TEK", "HWC", "PRED",
"METH", "DIS", "ANA", "POPGEN", "EVO", "RESIMP", "ISSUE", "PROT",
"PA", "PEFF"), row.names = c(NA, -43L), class = "data.frame")

Counting frequencies of categorical parameters, total and by groups, and appending the output to existing matrices

In my data (DPUT below) I have many categorical binary parameters holding various information about scientific papers. These are all 0=absent/no or 1=present/yes. Does the study have government funding, yes or no? Is harvest discussed in the paper, yes or no? And so on, for more than 30 parameters. I also have the nationality of the lead author. One thing I am trying to do is to have a look at the total fraction of studies being funded from governments (GVT), and the same fraction per country.
This code gives a matrix on the number of studies not funded by GVT, and those funded by GVT:
gvt_funding=aggregate(data.frame(count=sysrevt$GVT),list(value=sysrevt$GVT),length)
which gives the matrix:
value count
0 32
1 66
Then I calculate the percentage of total:
gvt.not=format(round(((gvt_funding[1,2]/(gvt_funding[1,2]+
gvt_funding[2,2]))*100),1),nsmall=1)
gvt.yes=format(round(((gvt_funding[2,2]/(gvt_funding[1,2]+
gvt_funding[2,2]))*100),1),nsmall=1)
Now I want to append these percentages in a new column in the matrix, change the name of these two columns to "TOTAL"and "TOTAL%", and then to calculate the same ratios and percentages for all nations (as shown by the column "LEAD"), and append these ratios and percentages to the same matrix with matching column titles.
It is quite possible that there are several more elegant ways to do this, but this is as far as I have come...
Here's the data:
structure(list(YR = c(2015, 2015, 2015, 2015, 2015, 2015, 2015,
2015, 2015, 2015, 2015, 2015, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2013, 2013, 2013, 2013, 2013, 2013,
2013, 2013, 2013), NRAUTH = c(6, 7, 3, 22, 10, 4, 23, 4, 4, 11,
9, 6, 7, 9, 4, 6, 1, 3, 5, 4, 7, 5, 3, 2, 2, 4, 7, 4, 26, 6,
2, 4, 6, 5, 7, 5, 7, 3, 3, 2, 6, 3, 3), LEAD = structure(c(12L,
5L, 2L, 2L, 12L, 12L, 12L, 11L, 12L, 2L, 5L, 5L, 4L, 4L, 12L,
12L, 11L, 9L, 2L, 2L, 12L, 12L, 1L, 8L, 2L, 12L, 12L, 10L, 4L,
1L, 6L, 12L, 12L, 12L, 4L, 5L, 7L, 2L, 3L, 12L, 12L, 2L, 2L), .Label = c("AUS",
"CAN", "COS", "DEU", "DNK", "GBR", "GRL", "KOR", "NOR", "POL",
"RUS", "USA"), class = "factor"), CAN = c(0, 1, 1, 1, 1, 1, 1,
0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1), DNK = c(0, 1, 0,
0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0), GRL = c(0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, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), RESIMP = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
), ISSUE = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0), PROT = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), PA = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), PEFF = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("YR",
"NRAUTH", "LEAD", "CAN", "DNK", "GRL", "USA", "NOR", "RUS", "NATX",
"ALL", "AB", "BB", "BS", "CS", "DS", "EG", "FB", "GB", "KB",
"KS", "LS", "LP", "MC", "NB", "NW", "SB", "SH", "VM", "WH", "GVT",
"NGO", "COM", "ACA", "FUNDX", "FUNDNN", "POPSTAT", "POPABU",
"POPTR", "BOUND", "HARV", "CC", "HAB", "HABP", "POLL", "SHIP",
"TOUR", "BEH", "REPEC", "ZOO", "PHYS", "TEK", "HWC", "PRED",
"METH", "DIS", "ANA", "POPGEN", "EVO", "RESIMP", "ISSUE", "PROT",
"PA", "PEFF"), row.names = c(NA, -43L), class = "data.frame")
Like this?
tab <- addmargins(table(sysrevt$LEAD, sysrevt$GVT), 1)
tab <- cbind(tab, 100 * prop.table(tab, 1))
colnames(tab) <- c('gvt.not', 'gvt.yes', 'gvt.not(%)', 'gvt.yes(%)')
rownames(tab)[nrow(tab)] <- 'TOTAL'
round(tab, 2)
The output is
gvt.not gvt.yes gvt.not(%) gvt.yes(%)
AUS 1 1 50.00 50.00
CAN 1 8 11.11 88.89
COS 1 0 100.00 0.00
DEU 1 3 25.00 75.00
DNK 0 4 0.00 100.00
GBR 1 0 100.00 0.00
GRL 0 1 0.00 100.00
KOR 1 0 100.00 0.00
NOR 0 1 0.00 100.00
POL 1 0 100.00 0.00
RUS 1 1 50.00 50.00
USA 7 9 43.75 56.25
TOTAL 15 28 34.88 65.12

Resources