So I have tried fitting a Plackett-Luce model to my matrix. Everything seems to be working fine untill I'm trying to calculate the Quasi-variance of the model and recieve the following error-message:
Error in X %*% as.vector(coefs) : Cholmod error 'X and/or Y have wrong dimensions' at file ../MatrixOps/cholmod_sdmult.c, line 88.
R <- as.rankings(ordered.matrix, input="orderings")
mod <- PlackettLuce(R, npseudo = 0.5, as.grouped_rankings=TRUE)
avRank <- apply(R, 2, function(x) mean(x[x > 0]))
coefs <- round(coef(mod)[order(avRank)], 2)
coefs
131 6 3 9 10 208 5 15 1 209
1.32 0.82 0.48 0.51 0.48 0.23 0.21 0.42 0.00 -0.37
qv <- qvcalc(mod)
Error in X %*% as.vector(coefs) :
Cholmod error 'X and/or Y have wrong dimensions' at file ../MatrixOps/cholmod_sdmult.c, line 88
Anyone who know's what the problem might be? Cheers
Related
After try to find a solution, I didn't.
I have a .txt file with a correlation matrix which was previously created from other records. It looks like this:
CXCL9 IL2RG TAP1
CXCL9 1
IL2RG 0.828 1
TAP1 0.605 0.631 1
CD274 0.564 0.57 0.679
LAG3 0.624 0.676 0.681
I am trying to generate a correlogram, an for that I've done this:
m <- read.table("file.txt", sep="\t", header=TRUE, check.names = FALSE)
mymatrix <- as.matrix(m)
corrplot(mymatrix, type = "lower", method="number")
And I get this message:
Error in corrplot(mymatrix, type = "lower") : The matrix is not in [-1, 1]!
How can I do a simple correlogram with this data? (maybe doing a heatmap?)
The desired output:
I’ve been having some trouble with the plotCalibration() function, I have managed to get it to work before, but recently whilst working with another dataset (here is a link to the .Rda data file), I have been unable to shake off an error message which keeps cropping up:
> plotCalibration(data = data, cOutcome = 2, predRisk = data$sortmort)
Error in plotCalibration(data = data, cOutcome = 2, predRisk = data$sortmort) : The specified outcome is not a binary variable.`
When I’ve tried to set the cOutcome column to factors or to logical, it still doesn’t work.
I’ve looked at the source of the function and the only time the error message comes up is in the first if()else{} statement:
if (length(unique(y))!=2) {stop(" The specified outcome is not a binary variable.\n")}
else{
But I have checked that the length(unique(y)) is indeed ==2, and so don’t understand why the error message still crops up!
Be sure you're passing a dataframe to PlotCalibration. Passing a dplyr tibble can cause this error. Converting with the normal as.data.frame() worked for me.
Using the data you sent earlier, I do not see any error though:
Following output were produced along with a calibration plot:
> library(PredictABEL)
> plotCalibration(data = data, cOutcome = 2, predRisk = data$sortmort)
$Table_HLtest
total meanpred meanobs predicted observed
[0.000632,0.00129) 340 0.001 0.000 0.31 0
0.001287 198 0.001 0.000 0.25 0
[0.001374,0.00201) 283 0.002 0.004 0.53 1
0.002009 310 0.002 0.000 0.62 0
[0.002505,0.00409) 154 0.003 0.000 0.52 0
[0.004086,0.00793) 251 0.006 0.000 1.42 0
[0.007931,0.00998) 116 0.008 0.009 0.96 1
[0.009981,0.19545] 181 0.024 0.011 4.40 2
$Chi_square
[1] 4.906
$df
[1] 8
$p_value
[1] 0.7676
Please try using table(data[,2],useNA = "ifany") to see the number of levels of the outcome variable of your dataset.
The function plotCalibration will execute when the outcome is a binary variable (two levels).
I have a function tryCatch that outputs a p-value for different inputs defined in ind_gene.
Is there a way to search all inputs in ind_gene and get all outputs from tryCatch where p < 0.05
ind_gene <- which(rownames(matrix_cpm_spike_liver) == "hsa-miR-320c")
s1 <- tryCatch(survdiff(
Surv(as.numeric(as.character(ClinicalDataHep$new_death))[ind_clin],
ClinicalDataHep$death_event[ind_clin])~
event_rna[ind_gene,ind_tum]), error = function(e) return(NA))
> s1
Call:
survdiff(formula = Surv(as.numeric(as.character(ClinicalDataHep$new_death))[ind_clin],
ClinicalDataHep$death_event[ind_clin]) ~ event_rna[ind_gene,
ind_tum])
N Observed Expected (O-E)^2/E (O-E)^2/V
event_rna[ind_gene, ind_tum]=0 8 6 5.52 0.0420 0.0652
event_rna[ind_gene, ind_tum]=1 51 25 25.48 0.0091 0.0652
Chisq= 0.1 on 1 degrees of freedom, p= 0.798
The p-value can be computed like this:
p.val <- 1 - pchisq(s1$chisq, length(s1$n) - 1)
I'm trying to solve a two-component decay model in R using the nls function, but running into errors. The equation is:
Where t is time, Ctot is C1+C2, and p1 and p2 are known proportions of Ctot.
my data (dd) is:
> head(dd,n=15)
t Ctot
1 0.00 6.62
2 0.33 6.45
3 0.50 6.38
4 0.67 6.44
5 0.83 6.38
6 1.00 6.39
7 1.17 6.35
8 1.33 6.33
9 1.50 6.33
10 1.67 6.28
11 1.83 6.17
12 2.00 6.11
13 2.17 6.07
14 2.33 5.89
15 2.50 5.86
Using nls I have tried:
p1 <- 0.3
p2 <- 0.7
z <- nls(Ctot~(p1*C1*(exp(-k1*t)))+(p2*C2*(exp(-k2*t))), data=dd, start=list(C1=6, C2=0.1, k1=0.01, k2=0.01))
However I am getting:
z <- nls(Ctot~(p1*C1*(exp(-k1*t)))+(p2*C2*(exp(-k2*t))), data=dd, start=list(C1=6, C2=0.1, k1=0.01, k2=0.01))
Error in numericDeriv(form[[3L]], names(ind), env) :
Missing value or an infinity produced when evaluating the model
I would be grateful if anyone has suggestions!
The data seems fairly limited and clearly incomplete since it only the head. If we make up some data for testing methods ... and leave out the confusing p1 and p2:
t=seq(0, 20, by=.3)
Ctot = 3 * exp( -1 * t) + 4 * exp(-5*t)
# following hte example on gnm::gnm's help page:
saved.fits <- list(); library(gnm)
for (i in 1:10) {
saved.fits[[i]] <- suppressWarnings( gnm(Ctot ~ Exp(1 + t, inst = 1) +
Exp(1 + t, inst = 2),
verbose=FALSE))}
plot(Ctot~t)
lines(saved.fits[[3]]$fitted~t)
lines(saved.fits[[3]]$fitted~t,col="red")
I wasn't familiar with the gnm package and so ended up reading the first few sections and then the worked 2 component data fitting example in its vignette: https://cran.r-project.org/web/packages/gnm/vignettes/gnmOverview.pdf . Most of the fits will be as expected, but some will find a local maximum in likelihood that is not a global max:
> saved.fits[[1]]$coefficients
(Intercept) Exp(. + t, inst = 1).(Intercept)
1.479909e-12 1.098612e+00
Exp(1 + ., inst = 1).t Exp(. + t, inst = 2).(Intercept)
-1.000000e+00 1.386294e+00
Exp(1 + ., inst = 2).t
-5.000000e+00
attr(,"eliminated")
[1] 0
> exp( saved.fits[[1]]$coefficients[4] )
Exp(. + t, inst = 2).(Intercept)
4
> exp( saved.fits[[1]]$coefficients[2] )
Exp(. + t, inst = 1).(Intercept)
3
With the data shown in the question it does not seem to work well but if you are open to other parametric models then this 3 parameter model seems reasonable.
fm <- nls(Ctot ~ 1 / (a + b * t^c), dd, st = list(a = 1, b = 1, c = 1))
plot(dd)
lines(fitted(fm) ~ t, dd, col = "red")
I am trying to add all the elements in a matrix. This is an example of my matrix (the actual matrix is bigger):
m = matrix(c(528,479,538,603),nrow=2,ncol=2)
m
A B
male 528 538
female 479 603
I am trying to do:
sum.elements = colSums(colSums(m))
but it gives the following error:
Error in colSums(colSums(m)) : 'x' must be an array of at least two
dimensions
I have tried doing:
x = colSums(m)
sum.elements = x[1] + x[2]
but this would be very long when you have a 100-column matrix...
Any help would be greatly appreciated!
You can do sum. It also has the option na.rm to remove the NA values.
sum(m)
#[1] 2148
In general, sum works for vector, matrix and data.frame
Benchmarks
set.seed(24)
m1 <- matrix(sample(0:20, 5000*5000, replace=TRUE), ncol=5000)
system.time(sum(m1))
# user system elapsed
# 0.027 0.000 0.026
system.time(sum(colSums(m1)))
# user system elapsed
# 0.027 0.000 0.027
system.time(Reduce('+', m1))
# user system elapsed
#25.977 0.644 26.673
Reduce will work
Reduce(`+`,m)
[1] 2148