I am having trouble extracting rownames from an object.
When I type in rownames(object), I obtain "null", but if I type in object, I obtain the matrix of information. If it helps, when I type in class(object), it tells me that it is a list. What I am looking for is a method to obtain the row names on the side. Thanks!
>
alpha84 alpha91 alpha98 alpha105 alpha112 alpha119
YBR088C 1.08 0.27 0.04 -0.51 -0.80 -0.89
YDL003W 0.62 -0.01 -0.36 -0.04 -0.55 -0.55
YDR097C 0.64 0.18 -0.05 0.03 -0.76 -0.66
YDR507C 0.53 0.13 0.07 0.14 -0.56 -0.41
YER070W 0.73 0.20 0.00 0.11 -0.53 -0.72
YER095W 0.28 -0.05 -0.11 -0.13 -0.87 -0.90
YER111C 0.37 -0.19 -0.11 -0.54 -0.34 -0.47
YGR189C 0.81 0.12 0.15 -0.39 -0.60 -1.20
YKL045W 0.46 -0.27 -0.10 -0.23 -0.42 -1.21
YLR183C 0.96 0.14 0.28 -0.17 -0.14 -0.68
YML027W 0.50 -0.01 0.11 -0.33 -0.44 -0.94
YMR179W 0.42 0.04 -0.40 -0.47 -0.12 -0.61
YNL300W 0.79 0.33 0.54 -0.09 -0.31 -1.01
YOR074C 0.73 0.09 -0.27 -0.22 -0.62 -0.80
YPL163C 1.61 0.84 0.82 -0.09 -0.48 -0.97
YPL256C 1.10 0.56 0.18 -0.32 -0.38 -1.04
structure(list(4 = structure(list(alpha0 = c(-1.15, -1.22,
-0.72, -1.76, -1.46, -0.57, -1.21, -0.32, -0.8, -1.7, -1.72,
-1.3, -1.24, -1.14, -2.42, -1.41), alpha7 = c(-0.86, -0.74, -0.85,
-0.34, -0.76, 0.42, -0.26, -0.65, 0.01, -1.46, -0.66, 0.07, -0.78,
-0.31, -2.15, -0.69), alpha14 = c(1.21, 1.34, 0.54, 0.18, 1.08,
1.03, 1.36, 0.87, 0.86, 0.93, 1.73, 0.98, 0.31, 0.57, 0.66, 1.39
), alpha21 = c(1.62, 1.5, 1.04, 1.07, 1.5, 1.35, 1.37, 1.1, 0.84,
1.12, 1.29, 1.12, 1.46, 1.08, 1.98, 1.98), alpha28 = c(1.12,
0.63, 0.84, 0.37, 0.74, 0.64, 0.54, 1.17, 0.51, 0.91, 0.51, 0.13,
1.11, 1.17, 1.55, 0.74), alpha35 = c(0.16, 0.29, 0.24, 0.32,
0.47, 0.42, 0.18, 0.44, 0.14, 0.11, 0.28, 0.19, 0.62, 0.57, 0.78,
0.21), alpha42 = c(-0.44, -0.55, -0.64, -0.5, -0.7, -0.4, -0.85,
0.37, -0.4, 0, 0.23, -0.58, 0.07, 0.31, 0.14, -0.36), alpha49 = c(-0.93,
-0.65, -0.83, -0.25, -0.68, -0.9, -0.82, -0.93, -0.64, -0.73,
-0.55, -0.63, -0.23, -0.74, -0.94, -1.32), alpha56 = c(-1.23,
-0.76, -0.36, -0.48, -1.03, -0.73, -0.75, -1.45, -0.8, -0.9,
-0.97, -0.9, -0.58, -0.68, -1.03, -1.5), alpha63 = c(-0.62, -0.88,
-0.7, -0.25, -0.55, -0.47, 0.07, -0.57, 0.41, -0.46, -0.48, 0.09,
-1.01, -0.1, -1.5, -1.07), alpha70 = c(0.62, 0.69, 0.99, 0.79,
0.35, 0.2, 0.89, 0.15, 0.88, 0.85, 0.57, 0.54, -0.24, -0.38,
-0.03, 0.35), alpha77 = c(1.3, 1.25, 1.08, 0.97, 1.24, 0.78,
0.78, 0.92, 0.75, 0.93, 0.88, 1.44, 0.23, 0.75, 1.25, 1.57),
alpha84 = c(1.08, 0.62, 0.64, 0.53, 0.73, 0.28, 0.37, 0.81,
0.46, 0.96, 0.5, 0.42, 0.79, 0.73, 1.61, 1.1), alpha91 = c(0.27,
-0.01, 0.18, 0.13, 0.2, -0.05, -0.19, 0.12, -0.27, 0.14,
-0.01, 0.04, 0.33, 0.09, 0.84, 0.56), alpha98 = c(0.04, -0.36,
-0.05, 0.07, 0, -0.11, -0.11, 0.15, -0.1, 0.28, 0.11, -0.4,
0.54, -0.27, 0.82, 0.18), alpha105 = c(-0.51, -0.04, 0.03,
0.14, 0.11, -0.13, -0.54, -0.39, -0.23, -0.17, -0.33, -0.47,
-0.09, -0.22, -0.09, -0.32), alpha112 = c(-0.8, -0.55, -0.76,
-0.56, -0.53, -0.87, -0.34, -0.6, -0.42, -0.14, -0.44, -0.12,
-0.31, -0.62, -0.48, -0.38), alpha119 = c(-0.89, -0.55, -0.66,
-0.41, -0.72, -0.9, -0.47, -1.2, -1.21, -0.68, -0.94, -0.61,
-1.01, -0.8, -0.97, -1.04)), .Names = c("alpha0", "alpha7",
"alpha14", "alpha21", "alpha28", "alpha35", "alpha42", "alpha49",
"alpha56", "alpha63", "alpha70", "alpha77", "alpha84", "alpha91",
"alpha98", "alpha105", "alpha112", "alpha119"), row.names = c("YBR088C",
"YDL003W", "YDR097C", "YDR507C", "YER070W", "YER095W", "YER111C",
"YGR189C", "YKL045W", "YLR183C", "YML027W", "YMR179W", "YNL300W",
"YOR074C", "YPL163C", "YPL256C"), class = "data.frame")), .Names = "4")
You have a list of one element. This single element is a data.frame.
If you are after the rownames from this object, then index the list appropriately
rownames(object[[1]])
## [1] "YBR088C" "YDL003W" "YDR097C" "YDR507C" "YER070W" "YER095W" "YER111C" "YGR189C" "YKL045W" "YLR183C" "YML027W"
## [12] "YMR179W" "YNL300W" "YOR074C" "YPL163C" "YPL256C"
For a more general list of data.frames
# get rownames from all data.frames in a list
lapply(object, rownames)
If you want a data.frame, not a list containing a data.frame then you could simply assign the results from the first element to a separate element
object.df <- object[[1]]
If it is a list of data.frames, it probably is more idiomatically R to keep it in the list, and use lapply to work on each element.
Related
I used a for loop to create a correlation matrix, because I needed to use polychor to generate polychoric correaltions and I was only able to get polychor to correlate two variables at a time. Anyway, I created my own correlation table with the following code:
for(i in 1:ncol(gd2)) {
for (j in 1:ncol(gd2)) {
corVal
The table looks like this:
head(dtnew)
Better Afraid Alive Bored Drop Empty Energy Happy Help Home Hope Memory Satis Spirit Worth TOT
1: 1.00 0.32 0.29 0.39 0.36 0.46 0.25 0.43 0.39 0.13 0.46 0.39 0.50 0.45 0.48 0.67
2: 0.32 1.00 0.25 0.20 0.24 0.30 0.23 0.30 0.43 0.15 0.44 0.28 0.31 0.29 0.34 0.62
3: 0.29 0.25 1.00 0.26 0.28 0.46 0.38 0.60 0.35 0.19 0.41 0.10 0.49 0.53 0.43 0.65
4: 0.39 0.20 0.26 1.00 0.36 0.56 0.31 0.36 0.39 0.16 0.32 0.23 0.39 0.35 0.44 0.67
5: 0.36 0.24 0.28 0.36 1.00 0.44 0.41 0.37 0.43 0.31 0.35 0.22 0.42 0.37 0.40 0.72
6: 0.46 0.30 0.46 0.56 0.44 1.00 0.32 0.55 0.51 0.18 0.45 0.17 0.62 0.52 0.64 0.75
>
But longer.
Here is the dput()
structure(list(Better = c(1, 0.32, 0.29, 0.39, 0.36, 0.46, 0.25,
0.43, 0.39, 0.13, 0.46, 0.39, 0.5, 0.45, 0.48, 0.67), Afraid = c(0.32,
1, 0.25, 0.2, 0.24, 0.3, 0.23, 0.3, 0.43, 0.15, 0.44, 0.28, 0.31,
0.29, 0.34, 0.62), Alive = c(0.29, 0.25, 1, 0.26, 0.28, 0.46,
0.38, 0.6, 0.35, 0.19, 0.41, 0.1, 0.49, 0.53, 0.43, 0.65), Bored = c(0.39,
0.2, 0.26, 1, 0.36, 0.56, 0.31, 0.36, 0.39, 0.16, 0.32, 0.23,
0.39, 0.35, 0.44, 0.67), Drop = c(0.36, 0.24, 0.28, 0.36, 1,
0.44, 0.41, 0.37, 0.43, 0.31, 0.35, 0.22, 0.42, 0.37, 0.4, 0.72
), Empty = c(0.46, 0.3, 0.46, 0.56, 0.44, 1, 0.32, 0.55, 0.51,
0.18, 0.45, 0.17, 0.62, 0.52, 0.64, 0.75), Energy = c(0.25, 0.23,
0.38, 0.31, 0.41, 0.32, 1, 0.48, 0.37, 0.36, 0.31, 0.14, 0.4,
0.43, 0.38, 0.74), Happy = c(0.43, 0.3, 0.6, 0.36, 0.37, 0.55,
0.48, 1, 0.45, 0.21, 0.49, 0.22, 0.69, 0.84, 0.49, 0.8), Help = c(0.39,
0.43, 0.35, 0.39, 0.43, 0.51, 0.37, 0.45, 1, 0.2, 0.51, 0.32,
0.5, 0.44, 0.6, 0.73), Home = c(0.13, 0.15, 0.19, 0.16, 0.31,
0.18, 0.36, 0.21, 0.2, 1, 0.23, 0.13, 0.13, 0.15, 0.26, 0.63),
Hope = c(0.46, 0.44, 0.41, 0.32, 0.35, 0.45, 0.31, 0.49,
0.51, 0.23, 1, 0.38, 0.48, 0.47, 0.59, 0.73), Memory = c(0.39,
0.28, 0.1, 0.23, 0.22, 0.17, 0.14, 0.22, 0.32, 0.13, 0.38,
1, 0.25, 0.24, 0.31, 0.66), Satis = c(0.5, 0.31, 0.49, 0.39,
0.42, 0.62, 0.4, 0.69, 0.5, 0.13, 0.48, 0.25, 1, 0.66, 0.6,
0.78), Spirit = c(0.45, 0.29, 0.53, 0.35, 0.37, 0.52, 0.43,
0.84, 0.44, 0.15, 0.47, 0.24, 0.66, 1, 0.51, 0.77), Worth = c(0.48,
0.34, 0.43, 0.44, 0.4, 0.64, 0.38, 0.49, 0.6, 0.26, 0.59,
0.31, 0.6, 0.51, 1, 0.77), TOT = c(0.67, 0.62, 0.65, 0.67,
0.72, 0.75, 0.74, 0.8, 0.73, 0.63, 0.73, 0.66, 0.78, 0.77,
0.77, 0.89)), row.names = c(NA, -16L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x000001d7adc21ef0>)
</pre/>
I would like to generate a visual using corrplot. However, when I try, I get an error:
Error in is.finite(tmp) : default method not implemented for type 'list'
My data is indeed of type list. I have tried usuing 'unlist'. Not sure what else to try.
There is a problem with your dput() output, possibly because you have a data.table. I can read it by deleting ", .internal.selfref = <pointer: 0x000001d7adc21ef0>" from the last line so that it ends class = c("data.table", "data.frame")). Printing that out shows a problem with the last line/column (Tot). The bottom row in that column should be 1.00, but it is 0.89. We can trim that and use as.matrix (my mistake in the earlier comment) to convert the data frame:
gd3 <- gd2[-16, -16]
corrplot(as.matrix(gd3))
library(corrplot)
M <- cor(df)
head(round(M,2))
corrplot(M, method="number")
I have a table with numbers and can plot a 3d histogram in excel.
Here is my histogram in excel:
How can i do the same in R with plot3d?
In their example they are use 3 digits for x, y, z.
Here their dataset and histogram in R:
But i have only one digit for one bar
My table:
-2.88 -1.76 -0.41 -2.25 -0.83 -0.62 -1.25 -2.68 -2.41 -1.74 -2.51 -0.78 -1.97 -2.67 -1.41 -1.56 0.49 -1.54 -1.37 -1.47 -2.32 0.66
-2.39 -1.98 -0.65 -2.33 -1.98 -1.19 -2.44 -2.13 -2.16 -2.44 -2.20 -1.77 -0.60 -0.73 -0.77 -1.59 -1.01 -1.37 -1.68 -0.92 -1.28 -0.12
-1.99 -2.48 -0.43 -1.75 -1.81 -2.37 -1.08 -1.18 -0.80 -3.30 -2.04 -1.96 -0.65 -2.44 -0.83 -1.67 -0.48 -1.03 -1.76 0.04 -1.30 -0.71
-2.73 -2.22 -0.98 -1.24 -2.21 -1.29 -1.37 -0.89 -0.86 -2.22 -1.32 -2.13 -1.04 -1.12 -0.60 -1.58 0.20 0.01 -1.81 -0.17 -0.38 -1.74
-1.63 -1.29 -1.31 -1.94 -2.39 -1.20 -1.66 -0.14 -0.96 -1.10 -0.40 -1.29 -0.44 -0.26 0.01 -2.71 -0.55 0.17 -3.44 -0.95 0.75 -1.08
-0.95 -0.15 -1.13 -1.18 -1.74 0.09 -1.12 -0.37 -0.80 -0.44 -1.18 -1.53 -1.28 0.36 -0.56 -1.54 -0.58 0.71 -1.53 -0.57 -0.91 -1.29
-0.67 0.02 -1.82 -0.84 -2.11 -0.38 -1.12 -0.57 -0.81 -1.04 -1.22 -0.93 -1.29 -0.26 0.02 -0.76 -0.28 -0.24 -0.43 -0.37 -1.30 -1.61
-3.45 -2.79 -0.44 -2.25 -0.81 -1.00 -1.20 -2.90 -1.96 -2.79 -2.91 -0.58 -1.65 -3.10 -1.23 -2.20 -0.15 -1.60 -1.51 -0.97 -2.35 0.38
-3.03 -3.12 -0.62 -2.01 -2.25 -1.84 -2.29 -2.51 -1.86 -2.93 -2.32 -1.63 -0.35 -1.05 -1.09 -2.04 -0.79 -1.18 -2.39 -0.54 -0.60 -0.71
-2.78 -2.60 -0.49 -1.69 -1.96 -2.10 -1.70 -1.26 -0.37 -2.80 -2.40 -2.23 -0.61 -2.26 -0.80 -2.11 -0.17 -0.21 -2.61 -0.09 -1.18 -1.26
-3.13 -1.96 -1.19 -1.17 -2.76 -0.87 -1.96 -0.22 -0.49 -2.75 -1.81 -2.48 -1.26 -1.04 0.08 -2.52 0.21 0.80 -2.28 -0.14 -0.27 -1.69
-1.52 -1.85 -1.36 -1.42 -2.28 -0.49 -1.58 -0.34 -1.11 -0.59 -0.74 -1.63 -0.58 -0.23 0.12 -2.97 0.17 0.68 -3.14 -0.64 0.21 -1.70
-1.05 -0.42 -1.50 -1.46 -2.32 -0.57 -0.63 -0.17 -0.79 -0.92 -1.52 -1.69 -1.25 0.34 -0.46 -1.94 0.27 0.82 -1.48 0.35 -1.25 -1.89
-1.03 0.28 -1.39 -0.82 -2.44 -0.75 -0.86 -0.69 -1.07 -1.38 -1.46 -1.09 -1.71 -0.50 0.59 -1.42 -0.54 -0.13 -0.86 -0.14 -1.28 -1.84
UPD:
I tried to insert a full dataset to one of examples. Just want to see how plot3 handle with a huge amount of bars. Its pretty stucking.
And i dont see a negative bars. I assume that positive bar will apperars upper 0 and negative bottom, like on my first picture.
So, i realize that firstly i need to render a big amount of data to be able to choose a right library.
Also i assume, that full realtime 3d rendering maybe impossible for that amount of data. So it will be normal if library will render just a 1 picture like a hist3d does.
m <- structure(c(-2.88, -1.76, -0.41, -2.25, -0.83, -0.62, -1.25, -2.68, -2.41, -1.74, -2.51, -0.78, -1.97, -2.67, -1.41, -1.56, 0.49, -1.54, -1.37, -1.47, -2.32, 0.66,
-2.39, -1.98, -0.65, -2.33, -1.98, -1.19, -2.44, -2.13, -2.16, -2.44, -2.20, -1.77, -0.60, -0.73, -0.77, -1.59, -1.01, -1.37, -1.68, -0.92, -1.28, -0.12,
-1.99, -2.48, -0.43, -1.75, -1.81, -2.37, -1.08, -1.18, -0.80, -3.30, -2.04, -1.96, -0.65, -2.44, -0.83, -1.67, -0.48, -1.03, -1.76, 0.04, -1.30, -0.71,
<=-=-=-=-=-=-=-=-=-=-=-skipped ==============>>
-2.64, -0.89, -1.60, -2.28, -3.56, -0.84, 0.31, 0.48, -0.31, 0.03, -2.42, 0.92, -3.10, -2.35, 0.03, -2.56, -0.91, 1.01, -5.90, -0.40, 2.95, -1.32,
-3.06, -0.69, -0.74, -2.46, -4.16, 0.46, 0.97, 0.46, -0.47, -0.79, -3.12, 1.09, -3.53, -1.08, -0.25, -1.26, -0.57, 0.67, -4.76, 0.01, -0.08, -1.56,
-2.70, -0.89, -0.97, -2.40, -5.45, -1.26, 1.65, 0.24, -1.60, -1.79, -2.05, 0.18, -3.01, -0.39, 0.47, -2.21, -0.50, 0.77, -3.05, 0.81, -0.36, -1.98), .Dim = c(700L, 22L))
library(graph3d)
dat <- cbind(
expand.grid(x = 1:700, y = 1:22),
z = c(m)
)
graph3d(
dat,
~x, ~y, ~z,
type = "bar"
)
Help me please to plot a histogram from a full txt file with positive up bars and negative down.
My full txt file is here https://pastebin.com/2zyyRDy8
I've read my txt file to res_cut, but i see data structure different from your examples, in my there 700 objs of 23 variable
res_cut <- read.delim("d:/result_cut.txt",sep = "\t", header = FALSE)
With the graph3d package:
m <- structure(c(-2.88, -2.39, -1.99, -2.73, -1.63, -0.95, -0.67,
-3.45, -3.03, -2.78, -3.13, -1.52, -1.05, -1.03, -1.76, -1.98,
-2.48, -2.22, -1.29, -0.15, 0.02, -2.79, -3.12, -2.6, -1.96,
-1.85, -0.42, 0.28, -0.41, -0.65, -0.43, -0.98, -1.31, -1.13,
-1.82, -0.44, -0.62, -0.49, -1.19, -1.36, -1.5, -1.39, -2.25,
-2.33, -1.75, -1.24, -1.94, -1.18, -0.84, -2.25, -2.01, -1.69,
-1.17, -1.42, -1.46, -0.82, -0.83, -1.98, -1.81, -2.21, -2.39,
-1.74, -2.11, -0.81, -2.25, -1.96, -2.76, -2.28, -2.32, -2.44,
-0.62, -1.19, -2.37, -1.29, -1.2, 0.09, -0.38, -1, -1.84, -2.1,
-0.87, -0.49, -0.57, -0.75, -1.25, -2.44, -1.08, -1.37, -1.66,
-1.12, -1.12, -1.2, -2.29, -1.7, -1.96, -1.58, -0.63, -0.86,
-2.68, -2.13, -1.18, -0.89, -0.14, -0.37, -0.57, -2.9, -2.51,
-1.26, -0.22, -0.34, -0.17, -0.69, -2.41, -2.16, -0.8, -0.86,
-0.96, -0.8, -0.81, -1.96, -1.86, -0.37, -0.49, -1.11, -0.79,
-1.07, -1.74, -2.44, -3.3, -2.22, -1.1, -0.44, -1.04, -2.79,
-2.93, -2.8, -2.75, -0.59, -0.92, -1.38, -2.51, -2.2, -2.04,
-1.32, -0.4, -1.18, -1.22, -2.91, -2.32, -2.4, -1.81, -0.74,
-1.52, -1.46, -0.78, -1.77, -1.96, -2.13, -1.29, -1.53, -0.93,
-0.58, -1.63, -2.23, -2.48, -1.63, -1.69, -1.09, -1.97, -0.6,
-0.65, -1.04, -0.44, -1.28, -1.29, -1.65, -0.35, -0.61, -1.26,
-0.58, -1.25, -1.71, -2.67, -0.73, -2.44, -1.12, -0.26, 0.36,
-0.26, -3.1, -1.05, -2.26, -1.04, -0.23, 0.34, -0.5, -1.41, -0.77,
-0.83, -0.6, 0.01, -0.56, 0.02, -1.23, -1.09, -0.8, 0.08, 0.12,
-0.46, 0.59, -1.56, -1.59, -1.67, -1.58, -2.71, -1.54, -0.76,
-2.2, -2.04, -2.11, -2.52, -2.97, -1.94, -1.42, 0.49, -1.01,
-0.48, 0.2, -0.55, -0.58, -0.28, -0.15, -0.79, -0.17, 0.21, 0.17,
0.27, -0.54, -1.54, -1.37, -1.03, 0.01, 0.17, 0.71, -0.24, -1.6,
-1.18, -0.21, 0.8, 0.68, 0.82, -0.13, -1.37, -1.68, -1.76, -1.81,
-3.44, -1.53, -0.43, -1.51, -2.39, -2.61, -2.28, -3.14, -1.48,
-0.86, -1.47, -0.92, 0.04, -0.17, -0.95, -0.57, -0.37, -0.97,
-0.54, -0.09, -0.14, -0.64, 0.35, -0.14, -2.32, -1.28, -1.3,
-0.38, 0.75, -0.91, -1.3, -2.35, -0.6, -1.18, -0.27, 0.21, -1.25,
-1.28, 0.66, -0.12, -0.71, -1.74, -1.08, -1.29, -1.61, 0.38,
-0.71, -1.26, -1.69, -1.7, -1.89, -1.84), .Dim = c(14L, 22L))
library(graph3d)
dat <- cbind(
expand.grid(x = 1:14, y = 1:22),
z = c(m)
)
graph3d(
dat,
~x, ~y, ~z,
type = "bar"
)
You could use hist3D from plot3Dpackage with z parameter:
m <- structure(c(-2.88, -2.39, -1.99, -2.73, -1.63, -0.95, -0.67,
-3.45, -3.03, -2.78, -3.13, -1.52, -1.05, -1.03, -1.76, -1.98,
-2.48, -2.22, -1.29, -0.15, 0.02, -2.79, -3.12, -2.6, -1.96,
-1.85, -0.42, 0.28, -0.41, -0.65, -0.43, -0.98, -1.31, -1.13,
-1.82, -0.44, -0.62, -0.49, -1.19, -1.36, -1.5, -1.39, -2.25,
-2.33, -1.75, -1.24, -1.94, -1.18, -0.84, -2.25, -2.01, -1.69,
-1.17, -1.42, -1.46, -0.82, -0.83, -1.98, -1.81, -2.21, -2.39,
-1.74, -2.11, -0.81, -2.25, -1.96, -2.76, -2.28, -2.32, -2.44,
-0.62, -1.19, -2.37, -1.29, -1.2, 0.09, -0.38, -1, -1.84, -2.1,
-0.87, -0.49, -0.57, -0.75, -1.25, -2.44, -1.08, -1.37, -1.66,
-1.12, -1.12, -1.2, -2.29, -1.7, -1.96, -1.58, -0.63, -0.86,
-2.68, -2.13, -1.18, -0.89, -0.14, -0.37, -0.57, -2.9, -2.51,
-1.26, -0.22, -0.34, -0.17, -0.69, -2.41, -2.16, -0.8, -0.86,
-0.96, -0.8, -0.81, -1.96, -1.86, -0.37, -0.49, -1.11, -0.79,
-1.07, -1.74, -2.44, -3.3, -2.22, -1.1, -0.44, -1.04, -2.79,
-2.93, -2.8, -2.75, -0.59, -0.92, -1.38, -2.51, -2.2, -2.04,
-1.32, -0.4, -1.18, -1.22, -2.91, -2.32, -2.4, -1.81, -0.74,
-1.52, -1.46, -0.78, -1.77, -1.96, -2.13, -1.29, -1.53, -0.93,
-0.58, -1.63, -2.23, -2.48, -1.63, -1.69, -1.09, -1.97, -0.6,
-0.65, -1.04, -0.44, -1.28, -1.29, -1.65, -0.35, -0.61, -1.26,
-0.58, -1.25, -1.71, -2.67, -0.73, -2.44, -1.12, -0.26, 0.36,
-0.26, -3.1, -1.05, -2.26, -1.04, -0.23, 0.34, -0.5, -1.41, -0.77,
-0.83, -0.6, 0.01, -0.56, 0.02, -1.23, -1.09, -0.8, 0.08, 0.12,
-0.46, 0.59, -1.56, -1.59, -1.67, -1.58, -2.71, -1.54, -0.76,
-2.2, -2.04, -2.11, -2.52, -2.97, -1.94, -1.42, 0.49, -1.01,
-0.48, 0.2, -0.55, -0.58, -0.28, -0.15, -0.79, -0.17, 0.21, 0.17,
0.27, -0.54, -1.54, -1.37, -1.03, 0.01, 0.17, 0.71, -0.24, -1.6,
-1.18, -0.21, 0.8, 0.68, 0.82, -0.13, -1.37, -1.68, -1.76, -1.81,
-3.44, -1.53, -0.43, -1.51, -2.39, -2.61, -2.28, -3.14, -1.48,
-0.86, -1.47, -0.92, 0.04, -0.17, -0.95, -0.57, -0.37, -0.97,
-0.54, -0.09, -0.14, -0.64, 0.35, -0.14, -2.32, -1.28, -1.3,
-0.38, 0.75, -0.91, -1.3, -2.35, -0.6, -1.18, -0.27, 0.21, -1.25,
-1.28, 0.66, -0.12, -0.71, -1.74, -1.08, -1.29, -1.61, 0.38,
-0.71, -1.26, -1.69, -1.7, -1.89, -1.84), .Dim = c(14L, 22L))
plot3D::hist3D(z=m)
I'm having difficulties about doing a CC analysis in R.
The assignment which I'm doing is from "Applied Multivariate Analysis" by Sharma, exercise 13.7, if you're familiar with it.
Basically, I'm asked to conduct a CCA on a set of variables. There are seven X variables, but only five Y variables, thus R complains that the dimensions are not compatible. See the image below for a visual representation of the data called CETNEW.
Edited (Changed from image to dput):
structure(list(...
1 = c("X1", "X2", "X3", "X4", "X5", "X6", "X7", "Y1", "Y2", "Y3", "Y4", "Y5"),
2 = c(2.72, 1.2, 0.82, 0.92, 1.19, 1, 1.45, 0.68, 0.98, 0.57, 1.07, 0.91), ...
3 = c(1.2, 3.78, 0.7, 1.04, 1.06, 1.32, 1.31, 0.56, 1, 0.79, 1.13, 1.38), ...
4 = c(0.82, 0.7, 1.7, 0.59, 0.83, 1.08, 1.01, 0.65, 0.78, 0.66, 0.93, 0.77), ...
5 = c(0.92, 1.04, 0.59, 3.09, 1.06, 0.93, 1.47, 0.62, 1.26, 0.51, 0.94, 0.85), ...
6 = c(1.19, 1.06, 0.83, 1.06, 2.94, 1.36, 1.66, 0.68, 1.16, 0.77, 1.37, 1.11), ...
7 = c(1, 1.32, 1.08, 0.93, 1.36, 2.94, 1.56, 0.9, 1.23, 0.78, 1.65, 1.31), ...
8 = c(1.45, 1.31, 1.01, 1.47, 1.66, 1.56, 3.11, 1.03, 1.7, 0.81, 1.63, 1.44), ...
9 = c(0.68, 0.56, 0.65, 0.62, 0.68, 0.9, 1.03, 1.71, 0.99, 0.65, 0.86, 0.72), ...
10 = c(0.98, 1, 0.78, 1.26, 1.16, 1.23, 1.7, 0.99, 3.07, 0.61, 1.43, 1.28), ...
11 = c(0.57, 0.79, 0.66, 0.51, 0.77, 0.78, 0.81, 0.65, 0.61, 2.83, 1.04, 0.84), ...
12 = c(1.07, 1.13, 0.93, 0.94, 1.37, 1.65, 1.63, 0.86, 1.43, 1.04, 2.83, 1.6), ...
13 = c(0.91, 1.38, 0.77, 0.85, 1.11, 1.31, 1.44, 0.72, 1.28, 0.84, 1.6, 4.01)),
row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"))
What I've Done so Far
CETNEW <- CETNEW[,-1] #To remove the non-numeric values
Create two variables (criterion and predictor variables) as:
CETNEWx <- CETNEW[1:7,]
CETNEWy <- CETNEW[8:12,]
Then I've been using various packages such as CCA, CCP and candisk. From CCA:
ccCETNEW <- cc(CETNEWx,CETNEWy)
Yields the following error message:
Error in cov(X, Y, use = "pairwise") : incompatible dimensions
The matcor function also from CCA, yields the following error message:
Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 7, 5
Thus, it would seem that it all boils down to the different dimension problem. I've talked to my professor about it, but since he is using SAS, which apparently are compatible with this problem and could solve it, he could not help me.
Please, if you're familiar with canonical correlation and have had a similar problem before, any help regarding this topic is highly appreciated.
If you look at your data, notice the first column is divided into X and Y labels. That suggests to me that your data are transposed. If so, each column is an observation and the X and Y labels indicate various measurements taken on each observation. Canonical correlations are performed on two groups of measurements/variables from a single set of observations. First, here is the transposed data:
CETNEW.T <- structure(list(X1 = c(2.72, 1.2, 0.82, 0.92, 1.19, 1, 1.45, 0.68,
0.98, 0.57, 1.07, 0.91), X2 = c(1.2, 3.78, 0.7, 1.04, 1.06, 1.32,
1.31, 0.56, 1, 0.79, 1.13, 1.38), X3 = c(0.82, 0.7, 1.7, 0.59,
0.83, 1.08, 1.01, 0.65, 0.78, 0.66, 0.93, 0.77), X4 = c(0.92,
1.04, 0.59, 3.09, 1.06, 0.93, 1.47, 0.62, 1.26, 0.51, 0.94, 0.85
), X5 = c(1.19, 1.06, 0.83, 1.06, 2.94, 1.36, 1.66, 0.68, 1.16,
0.77, 1.37, 1.11), X6 = c(1, 1.32, 1.08, 0.93, 1.36, 2.94, 1.56,
0.9, 1.23, 0.78, 1.65, 1.31), X7 = c(1.45, 1.31, 1.01, 1.47,
1.66, 1.56, 3.11, 1.03, 1.7, 0.81, 1.63, 1.44), Y1 = c(0.68,
0.56, 0.65, 0.62, 0.68, 0.9, 1.03, 1.71, 0.99, 0.65, 0.86, 0.72
), Y2 = c(0.98, 1, 0.78, 1.26, 1.16, 1.23, 1.7, 0.99, 3.07, 0.61,
1.43, 1.28), Y3 = c(0.57, 0.79, 0.66, 0.51, 0.77, 0.78, 0.81,
0.65, 0.61, 2.83, 1.04, 0.84), Y4 = c(1.07, 1.13, 0.93, 0.94,
1.37, 1.65, 1.63, 0.86, 1.43, 1.04, 2.83, 1.6), Y5 = c(0.91,
1.38, 0.77, 0.85, 1.11, 1.31, 1.44, 0.72, 1.28, 0.84, 1.6, 4.01
)), class = "data.frame", row.names = c(NA, -12L))
Now the analysis runs fine:
library("CCA")
str(CETNEW.T)
# 'data.frame': 12 obs. of 12 variables:
# $ X1: num 2.72 1.2 0.82 0.92 1.19 1 1.45 0.68 0.98 0.57 ...
# $ X2: num 1.2 3.78 0.7 1.04 1.06 1.32 1.31 0.56 1 0.79 ...
# $ X3: num 0.82 0.7 1.7 0.59 0.83 1.08 1.01 0.65 0.78 0.66 ...
# $ X4: num 0.92 1.04 0.59 3.09 1.06 0.93 1.47 0.62 1.26 0.51 ...
# $ X5: num 1.19 1.06 0.83 1.06 2.94 1.36 1.66 0.68 1.16 0.77 ...
# $ X6: num 1 1.32 1.08 0.93 1.36 2.94 1.56 0.9 1.23 0.78 ...
# $ X7: num 1.45 1.31 1.01 1.47 1.66 1.56 3.11 1.03 1.7 0.81 ...
# $ Y1: num 0.68 0.56 0.65 0.62 0.68 0.9 1.03 1.71 0.99 0.65 ...
# $ Y2: num 0.98 1 0.78 1.26 1.16 1.23 1.7 0.99 3.07 0.61 ...
# $ Y3: num 0.57 0.79 0.66 0.51 0.77 0.78 0.81 0.65 0.61 2.83 ...
# $ Y4: num 1.07 1.13 0.93 0.94 1.37 1.65 1.63 0.86 1.43 1.04 ...
# $ Y5: num 0.91 1.38 0.77 0.85 1.11 1.31 1.44 0.72 1.28 0.84 ...
X <- CETNEW.T[, 1:7]
Y <- CETNEW.T[, 8:12]
ccCETNEW <- cc(X, Y)
ccCETNEW is list with 5 parts containing the results.
I need to read the following matrix from a file. It's a symmetric correlation matrix, so half of it is omitted.
1.00
0.49 1.00
0.53 0.57 1.00
0.49 0.46 0.48 1.00
0.51 0.53 0.57 0.57 1.00
0.33 0.30 0.31 0.24 0.38 1.00
0.32 0.21 0.23 0.22 0.32 0.43 1.00
0.20 0.16 0.14 0.12 0.17 0.27 0.33 1.00
0.19 0.08 0.07 0.19 0.23 0.24 0.26 0.25 1.00
0.30 0.27 0.24 0.21 0.32 0.34 0.54 0.46 0.28 1.00
0.37 0.35 0.37 0.29 0.36 0.37 0.32 0.29 0.30 0.35 1.00
0.21 0.20 0.18 0.16 0.27 0.40 0.58 0.45 0.27 0.59 0.31 1.00
Currently, I'm using
data1 <- na.omit(as.vector(t(read.table('triangle-data.txt', fill = TRUE))))
pt <- 12
R <- matrix(0, nrow = pt , ncol = pt)
for(i in 1:pt){
R[i, 1:i] <- data1[(i*(i-1)/2 + 1): (i*(i+1)/2)]
}
R <- R + t(R) - diag(rep(1, pt))
R
The result is
> dput(R)
structure(c(1, 0.49, 0.53, 0.49, 0.51, 0.33, 0.32, 0.2, 0.19,
0.3, 0.37, 0.21, 0.49, 1, 0.57, 0.46, 0.53, 0.3, 0.21, 0.16,
0.08, 0.27, 0.35, 0.2, 0.53, 0.57, 1, 0.48, 0.57, 0.31, 0.23,
0.14, 0.07, 0.24, 0.37, 0.18, 0.49, 0.46, 0.48, 1, 0.57, 0.24,
0.22, 0.12, 0.19, 0.21, 0.29, 0.16, 0.51, 0.53, 0.57, 0.57, 1,
0.38, 0.32, 0.17, 0.23, 0.32, 0.36, 0.27, 0.33, 0.3, 0.31, 0.24,
0.38, 1, 0.43, 0.27, 0.24, 0.34, 0.37, 0.4, 0.32, 0.21, 0.23,
0.22, 0.32, 0.43, 1, 0.33, 0.26, 0.54, 0.32, 0.58, 0.2, 0.16,
0.14, 0.12, 0.17, 0.27, 0.33, 1, 0.25, 0.46, 0.29, 0.45, 0.19,
0.08, 0.07, 0.19, 0.23, 0.24, 0.26, 0.25, 1, 0.28, 0.3, 0.27,
0.3, 0.27, 0.24, 0.21, 0.32, 0.34, 0.54, 0.46, 0.28, 1, 0.35,
0.59, 0.37, 0.35, 0.37, 0.29, 0.36, 0.37, 0.32, 0.29, 0.3, 0.35,
1, 0.31, 0.21, 0.2, 0.18, 0.16, 0.27, 0.4, 0.58, 0.45, 0.27,
0.59, 0.31, 1), .Dim = c(12L, 12L))
This is too unwieldy, and I need to hard-code its size. Is there a more convenient way?
I used a combination of readLines and strsplit to read the file
a <- sapply(sapply(lapply(readLines("triangle.txt"),
function(x) strsplit(x, " ")), "[", 1),
function(x) na.omit(as.numeric(x)))
and rbind to cast it into a square matrix
A <- do.call("rbind", a)
Despite the warning, the lower part of the matrix is correctly read from the file, but the upper part is all messed up, which I fixed with a little dirty trick
A[upper.tri(A)] <- 0
A <- A + t(A) - diag(nrow(A))
EDIT
Another simpler solution based on the vector of the coefficients:
data1 <- na.omit(as.vector(t(read.table('triangle.txt', fill = TRUE))))
n <- Re(polyroot(c(-length(data1), 1/2, 1/2)))[1]
A <- matrix(0, n, n)
A[upper.tri(A, diag = T)] <- data1
A <- A + t(A) - diag(n)
I have the data shown below:
Location=c("lcn","lcn","lcn","etb","lcs","bbs","lcn","lcs","bbs","lcn","lcs","bbs","lcs","lcs","lcn",
"bbs","etb","bbs","etb","etb","lcs","lcn","lcn","bbs","bbs","etb","bbs","etb","bbs","bbs",
"lcs","lcs","lcs","lcs","lcs","lcn","lcs","etb","lcn","lcn","etb","etb","etb","etb","lcn",
"bbs","bbs","lcs","etb","lcs","bbs","bbs","lcs","bbs","lcs","lcn","lcn","lcn","etb","lcn",
"lcs","bbs","etb","etb","etb","bbs","etb","bbs","etb","etb","bbs","lcs")
Treatment=c(rep("control",each=21),rep("foam",each=20),rep("hail",each=17),rep("teda",each=14))
Growth=c( 0.24, -0.05, 0.19, 1.02, 0.84, 0.11, 0.13, 0.08, -0.18, -0.06,
0.38, 1.04, 0.55, -1.71, 0.24, 0.05, 0.49, -0.41, 0.70, 0.30,
1.03, 0.14, 0.73, 0.56, 0.56, 0.98, 0.53, 0.27, 0.32, 0.95,
0.10, 0.55, 1.18, 0.49, 0.58, 0.36, 0.18, 0.30, 1.71, 0.65,
0.69, 0.68, 0.66, 1.24, 0.47 , 1.28, 0.60, 1.01, 0.76, 1.35,
1.02, 0.75, 0.40, 0.37, 0.46, 0.47, 0.25, 0.61, 0.63, 0.86,
0.92, 0.09, 1.66, 0.88, 0.68, 1.02, 1.17, 1.18, 1.71, 1.01,
0.42, 0.56)
Mang=data.frame(Location,Treatment,Growth)
I want to use Two-Way Anova to see the influence of changing Location and Treatment on Growth. Two questions I want to ask:
(1) If some levels of predictors can't pass the normality test (shown below), can I still do the Anova ?
> shapiro.test(subset(Mang,Location=="lcn")[,3])$p.value
[1] 0.01317841
> shapiro.test(subset(Mang,Treatment=="control")[,3])$p.value
[1] 0.008312405
(2) Why the results are different when the order of predictors is changed in Anova?
> test1=aov(Growth~Location+Treatment,data=Mang)
> summary(test1)
Df Sum Sq Mean Sq F value Pr(>F)
Location 3 1.713 0.5710 2.708 0.05235 .
Treatment 3 3.495 1.1650 5.524 0.00193 **
Residuals 65 13.707 0.2109
---
Signif. codes: 0 ?**?0.001 ?*?0.01 ??0.05 ??0.1 ??1
> test2=aov(Growth~Treatment+Location,data=Mang)
> summary(test2)
Df Sum Sq Mean Sq F value Pr(>F)
Treatment 3 4.402 1.4673 6.958 0.000393 ***
Location 3 0.806 0.2687 1.274 0.290658
Residuals 65 13.707 0.2109
---
Signif. codes: 0 ?**?0.001 ?*?0.01 ??0.05 ??0.1 ??1