R: Confusion matrix from list of relevant variables - r

I am trying to set up a simulation but I struggle with something.
I know which variables are relevant or not in the beginning as I set them myself. They are stored in a beta matrix taking 0 if insignificant and 1 (same amplitude for all) if they are significant.
I run a procedure which gives me its list of relevant variables. The problem is that, this procedures is not giving me a 0-1 matrix but just a list of the selected variables using their "place" in number (so 38 for the 38th variable in my list). I know wish to test whether this procedure is powerful or not and I want to display the confusion matrix (among other things).
Below you can find those two vectors. I've tried using the "%in%" operator but it does not work and I don't want to use a loop as the simulation takes enough time already.
Thanks in advance!
res=c( 1, 9, 11 ,18, 19, 25, 26, 28, 31, 34 ,37 ,38, 39, 42 ,43 ,47 ,48, 50) #From my procedure
beta=c(1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1)

Related

What is difference Qt.matrix4x4 and QMatrix4x4?

I am learning the Qt 3D module. I found the Qt.matrix4x4 provide different API than QMatrix4x4 in C++ side (by using F1 check the API doc).
look likes Qt.matrix4x4 don't have such scale or rotation operations. even not in autocomplete.
BUT! these functions do exist and work!
var m = Qt.matrix4x4(1, 0, 0, -0.5, 0, 1, 0, 0, 0, 0, 1, 0.5, 0, 0, 0, 1)
m.scale(2)
console.log(m)
// qml: QMatrix4x4(2, 0, 0, -0.5, 0, 2, 0, 0, 0, 0, 2, 0.5, 0, 0, 0, 1)
// the type shows QMatrix4x4
https://doc.qt.io/qt-5/qmatrix4x4.html vs https://doc.qt.io/qt-5/qml-matrix4x4.html
I understand what these is, But why the API show different in API doc and autocomplete?
It is interesting to note that in Qt6 the documentation appears different than Qt5. In Qt6 we see clearly there are methods for constructing the matrix4x4 basic type from a vector and for multiplying them with vectors.
// translate(vector3d)
var m = Qt.matrix4x4();
m.translate(Qt.vector3d(1,2,3));
console.log(m.toString());
// QMatrix4x4(1, 0, 0, 1, 0, 1, 0, 2, 0, 0, 1, 3, 0, 0, 0, 1)
// matrix multiplication
var m = Qt.matrix4x4();
m.translate(Qt.vector3d(1,2,3));
console.log(m.toString());
// QMatrix4x4(1, 0, 0, 1, 0, 1, 0, 2, 0, 0, 1, 3, 0, 0, 0, 1)
// applying a matrix to a vector
var a = Qt.matrix4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
var b = Qt.vector3d(5,6,7);
var c = a.times(b);
console.log(c.toString()); // QVector3D(0.155556, 0.437037, 0.718518)
Note I tried all of the above in Qt5.15.6 but it doesn't work. So it appears that support for this type really kicks in at Qt6.
For further information see: https://doc.qt.io/qt-6/qml-matrix4x4.html

Multidimensional Scale in R - data

I would like to have a multidimensional scaling plot according to the following table (this is just a shorter form of the whole table).
I have been trying to do it in R (am quite new here...) but now. I am not even sure about that this type of data is good for multidimensional scaling. The whole table should mirror a semantic (linguistic) map (Thats why I thought that MDS should be good) and the rows mean that informants saw some pictures and gave different expressions (columns) for the pictures, so they described them differently.
The numbers in the columns are no judgments in the sense that they are on a scale from 1 to 10 or something like that but they show how many people used the expression for pic1, pic2, and so forth.
Could anyone help me to explain that MDS is actually the appropriate model I am trying to use? (Sorry, I am just too much confused after reading a lot in the last days about different methods...)
If so, here is the coding I used (just to be sure).
Thanks a lot for any advice!
daten <- structure(list(photos = c("p1", "p5", "p8", "p13", "p19", "p23", "p29", "p34", "p36", "p40", "p59", "p2", "p14"), expression1 = c(18, 8, 11, 15, 14, 16, 10, 12, 15, 18, 18, 0, 0), expression2 = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0), expression3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1), expression4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 17), expression5 = c(0, 3, 5, 0, 0, 0, 1, 5, 1, 0, 0, 0, 0), expression6 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), expression7 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), expression8 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -13L), class = c("tbl_df", "tbl", "data.frame"))
library("tibble")
has_rownames(daten)
cr<-column_to_rownames(daten, var="photo")
has_rownames(cr)
matr_cr <- as.matrix(cr[,-1])
matr_cr
d<-dist(matr_cr)
fit <- cmdscale(d, eig = TRUE, k = 2)
x <- fit$points[, 1]
y <- fit$points[, 2]
plot(x, y, xlab="Coordinate 1", ylab="Coordinate 2",
main="Multidimensional Scaling", type="n")
text(x, y, labels = row.names(matr_cr), cex=.6, col="red")
cr
Plotting multidimensional data is difficult and depending on the type of data and analysis is what to do. First of all, if you have several variables, it may be useful to cluster your data, one possible method is k-means that you can find it in the package "ClusterR". Another possible thing to do is to transform your variable by rotating the axis in order to lower the dimension with a Principal Component Analysis (PCA), you can find more about PCA in R in http://www.sthda.com/english/articles/31-principal-component-methods-in-r-practical-guide/
If you opp to plot your data as it is without a previous analysis, you may use ggplot2 package to make more useful and elegant plots. And to plot your different data attributes you can try changing size, color, shape, etc scales representing different dimensions. The problem with this option is that you can not plot several dimensions.
If I understand you well, you got pictures and people (informants) that make a review of the pictures. And the critics are separated in different levels (dimensions). If it is like that, you got as dimensions pictures, reviewers, and each level of the reviews, that make 2+N variables. Note that you can easily plot up to 5 dimensions in this kind of data, by setting x-axis and y-axis you got 2 dimensions, then you can use size scale for another dimension, color scale for another dimension, and the depending on your data and your preference you can use text or shape scale for the fifth dimension. I do not see in the table you provide the informants (reviewers) dimension. Further below you will found two examples of these plot using ggplot2, note that for shape scale a discrete variable must be used. In order to get beautiful plots and with meaning, you will have to try wich type of scale is better for each of your variables and will strongly depend on your data. Lastly, if you have several dimensions normally you should try first to assess if your data is clusterized or do a PCA.
library(ggplot2)
daten <- structure(list(photos = c("p1", "p5", "p8", "p13", "p19", "p23", "p29", "p34", "p36", "p40", "p59", "p2", "p14"), expression1 = c(18, 8, 11, 15, 14, 16, 10, 12, 15, 18, 18, 0, 0), expression2 = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0), expression3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1), expression4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 17), expression5 = c(0, 3, 5, 0, 0, 0, 1, 5, 1, 0, 0, 0, 0), expression6 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), expression7 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), expression8 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -13L), class = c("tbl_df", "tbl", "data.frame"))
# with shape scale
ggplot(data = daten,aes(x=photos, y=expression1, col=expression2, size=expression3, shape=as.factor(expression4))) +
geom_point()
# with text scale
ggplot(data = daten,aes(x=expression4, y=expression1, col=expression2, size=expression3, label=photos)) +
geom_text()

What does this error mean "order(vertex_attr(g, measure), decreasing = TRUE) : argument 1 is not a vector" in R?

I am trying to calculate robustness, a graph theory measure using R (braingraph package).
Robustness = robustness(my_networkgraph, type = c("vertex"), measure = ("btwn.cent"))
I get the following error, when I use the above robustness function:
Error in order(vertex_attr(g, measure), decreasing = TRUE) : argument 1 is not a vector
Any idea, what I am doing wrong here?
My network, which is a matrix has been converted to igraph object and robustness was calculated.
My network as a matrix:
mynetwork <- matrix(c(0, 1, 0, 1, 0, 0, 0, 0,
1, 0, 1, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 1, 1, 0, 1, 1,
0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0), nrow = 8)
This matrix was converted as igraph using the following code:
my_networkgraph <-graph_from_adjacency_matrix(mynetwork, mode = c("undirected"),weighted = NULL, diag = TRUE, add.colnames = NULL, add.rownames = NA)
Please help me to understand the above error
Thanks
Priya
There was a bug in the above function. To run the robustness code, you will need to supply a vertex attribute to your network: V(network)$degree <- degree(network) V(network)$btwn.cent <- centr_betw(network)$res

Using car::Anova package for a doubly-multivariate MANOVA in R

I'm trying to run a repeated-measures MANOVA in R, which also contains a number of dependent variables (key outcome variables of behavioural tasks). The repeated-measures are due to a cross-over design, in which individuals took a drug and placebo (in randomised order).
The code I'm running looks like this:
imatrix <- matrix(c(
1, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, -1,
0, 1, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, -1,
0, 0, 1, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, -1,
0, 0, 0, 1, 0, 0, 1,
0, 0, 0, 1, 0, 0, -1,
0, 0, 0, 0, 1, 0, 1,
0, 0, 0, 0, 1, 0, -1,
0, 0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 0, 1, -1
), 12, 7, byrow=TRUE)
colnames(imatrix) <- c("BCST", "CGT", "AST", "AGN", "DDT", "FERT", "NAC")
(imatrix <- list(measure=imatrix[,1:6], condition=imatrix[,7]))
contrasts(condition_factor) <- matrix(c(-1,1,1, -1), ncol=2)
doubly.mod<-lm(cbind(bcst_nac$totPersErr,bcst_placebo$totPersErr,cantab_nac$CGT.Delay.aversion,cantab_placebo$CGT.Delay.aversion,cantab_nac$AST.Switching.cost..Mean..correct.,cantab_placebo$AST.Switching.cost..Mean..correct.,cantab_nac$AGN.Affective.response.bias..Mean.,cantab_placebo$AGN.Affective.response.bias..Mean.,aucs_NAC,aucs_placebo,fert_nac$FERTACCHA,fert_placebo$FERTACCHA)~1))
Manova(doubly.mod, imatrix=imatrix, type =3)
The result is this error: Error in Anova.III.mlm(mod, SSPE, error.df, idata, idesign, icontrasts, :
(list) object cannot be coerced to type 'double'
However, when I change imatrix back from a list to a matrix, I get this error response:
Error in do.call(cbind, imatrix) : second argument must be a list
I've based this off the example from the car::Anova package about doubly multivariate analyses. Please let me know if you can help, or if I can add anything to make this question clearer.

how do I rebuild data frame based on columns identified in a numeric vector?

I'm using R to complete some GA driven searches.
Returned from my GA script is the resulting chromosome, returned as a binary numeric of length 40.
An example is: c(0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0).
I also have a corresponding data frame with 40 columns.
Using the data in the numeric vector, how do I efficiently build a (or re-build the) data frame so that it contains only those columns represented by the 1's in my numeric vector?
Building a sample data.frame and assigning your sample vector to x:
df <- as.data.frame(matrix(sample(1:100, 400, replace=T), ncol=40))
x <- c(0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0)
I can subset:
df[ ,x==1]
or:
df[, as.logical(x)]

Resources