Accessing a slot in the results of wilcox_test - r

I'm doing a test on some data:
wilcox_test(y ~ x, distribution="exact", conf.int=TRUE)
# Exact Wilcoxon Mann-Whitney Rank Sum Test
#
# data: y by x (1, 2)
# Z = 1.8732, p-value = 0.06106
# alternative hypothesis: true mu is not equal to 0
# 95 percent confidence interval:
# 0 2
# sample estimates:
# difference in location
# 1
The number displayed under difference in location is the Hodges-Lehmann estimator. I want to assign it to a variable for further calculation. But I do not know:
How can I access the value of the "difference in location"?
I looked at str(wilcox_test(y ~ x, distribution="exact", conf.int=TRUE)), which gives me:
Formal class 'ScalarIndependenceTestConfint' [package "coin"] with 7 slots
..# confint :function (level)
..# conf.level : num 0.95
..# nullvalue : num 0
..# distribution:Formal class 'ExactNullDistribution' [package "coin"] with 7 slots
.. .. ..# q :function (p)
.. .. ..# d :function (x)
.. .. ..# support :function ()
.. .. ..# parameters: list()
.. .. ..# pvalue :function (q)
.. .. ..# p :function (q)
.. .. ..# name : chr "exact distribution (via Streitberg-Roehmel algorithm)"
..# statistic :Formal class 'ScalarIndependenceTestStatistic' [package "coin"] with 14 slots
.. .. ..# alternative : chr "two.sided"
.. .. ..# teststatistic : Named num 1.87
.. .. .. ..- attr(*, "names")= chr "1"
.. .. ..# standardizedlinearstatistic: Named num 1.87
.. .. .. ..- attr(*, "names")= chr "1"
.. .. ..# linearstatistic : num 4246
.. .. ..# expectation : Named num 3875
.. .. .. ..- attr(*, "names")= chr "1"
.. .. ..# covariance :Formal class 'Variance' [package "coin"] with 1 slot
.. .. .. .. ..# variance: Named num 39334
.. .. .. .. .. ..- attr(*, "names")= chr "1"
.. .. ..# xtrans : num [1:124, 1] 1 1 1 1 1 1 1 1 1 1 ...
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:124] "1" "2" "3" "4" ...
.. .. .. .. ..$ : chr "1"
.. .. .. ..- attr(*, "assign")= int 1
.. .. ..# ytrans : num [1:124, 1] 114.5 93.5 9 50 114.5 ...
.. .. .. ..- attr(*, "assign")= int 1
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : NULL
.. .. .. .. ..$ : chr ""
.. .. ..# xtrafo :function (data, numeric_trafo = id_trafo, factor_trafo = f_trafo, ordered_trafo = of_trafo,
surv_trafo = logrank_trafo, var_trafo = NULL, block = NULL)
.. .. ..# ytrafo :function (data)
.. .. ..# x :'data.frame': 124 obs. of 1 variable:
.. .. .. ..$ x: Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
.. .. ..# y :'data.frame': 124 obs. of 1 variable:
.. .. .. ..$ y: num [1:124] 7 5 0 2 7 1 0 9 1 7 ...
.. .. ..# block : Factor w/ 1 level "0": 1 1 1 1 1 1 1 1 1 1 ...
.. .. ..# weights : num [1:124] 1 1 1 1 1 1 1 1 1 1 ...
..# estimates : list()
..# method : chr "Wilcoxon Mann-Whitney Rank Sum Test"
But wilcox_test(...)#estimates only returns:
list()
which appears empty.
So where is the 1 stored that is listed under difference in location in the results???
Additional explanation:
This works:
hle <- wilcox.test(x, y, paired = TRUE, correct = FALSE, conf.int = TRUE)$estimate
But that is a different test and not appropriate to my data.

I had to do a little bit of digging to figure this out since the difference in location value is nested down a couple of levels, but I'll walk you through it using an example object provided in the help file for wilcox_test - the object is named wt, and I'll include it below. For things like this, I usually start by inspecting the show method of the class of interest -
R> class(wt)
[1] "ScalarIndependenceTestConfint"
attr(,"package")
[1] "coin"
R> getMethod("show","ScalarIndependenceTestConfint")
Method Definition:
function (object)
{
x <- object
stat <- x#statistic#teststatistic
names(stat) <- "Z"
dist <- x#distribution
cld <- class(dist)
attributes(cld) <- NULL
distname <- switch(cld, AsymptNullDistribution = "Asymptotic",
ApproxNullDistribution = "Approximative", ExactNullDistribution = "Exact")
dataname <- varnames(x#statistic)
ci <- confint(object, level = object#conf.level)
RET <- list(statistic = stat, p.value = x#distribution#pvalue(stat),
alternative = x#statistic#alternative, method = paste(distname,
x#method), data.name = dataname, conf.int = ci$conf.int,
estimate = ci$estimate)
if (length(x#nullvalue))
RET$null.value = c(mu = x#nullvalue)
if (length(x#estimates))
RET <- c(RET, x#estimates)
class(RET) <- "htest"
print(RET)
invisible(RET)
}
<environment: namespace:coin>
The line of interest is the last item in the RET list - estimate = ci$estimate, which was calculated above as ci <- confint(object, level = object#conf.level). So with the example object, wt.ci <- confint(wt, level=.95), you can do
R> wt.ci$estimate
difference in location
-0.305
which matches
R> wt
Exact Wilcoxon Mann-Whitney Rank Sum Test
data: pd by age (12-26 Weeks, At term)
Z = -1.2247, p-value = 0.2544
alternative hypothesis: true mu is not equal to 0
95 percent confidence interval:
-0.76 0.15
sample estimates:
difference in location
-0.305
From the help file ?wilcox_test():
water_transfer <- data.frame(
pd = c(0.80, 0.83, 1.89, 1.04,
1.45, 1.38, 1.91, 1.64, 0.73, 1.46,
1.15, 0.88, 0.90, 0.74, 1.21),
age = factor(c(rep("At term", 10), rep("12-26 Weeks", 5))))
##
wt <- wilcox_test(pd ~ age, data = water_transfer,
distribution = "exact", conf.int = TRUE)

Related

Convert from spatialpixeldataframe to estUD

In short: I have a spatialpixeldataframe of a utilization distribution of an animal across a grid (udspdf) which I need to convert to estUD-class.
Background to how I got there: After calculating the KDE of a single animal, I then need to convert the estUD-class object to a spatialpixeldataframe in order to remove non-habitat cells, and rescale the utilisation distribution of the habitat-cells to 1. I then need to convert the spatialpixeldataframe back to an estUD class file so that I can run kerneloverlaphr() on it.
I have the following code, which converts it to an estUDm-class. But I need it in estUD-class, as there is only one animal.
re <- lapply(1:ncol(udspdf), function(i) {
so <- new("estUD", udspdf[,i])
so#h <- list(h=0, meth="specified") # specify dummy h values: they are only required to recreate the estUDm
so#vol <- FALSE
return(so)
})
names(re) <- names(udspdf)
class(re) <- "estUDm"
image(re)
If I just change
class(re) <- "estUD"
This seems to work, but then I can see there is an issue because
image(re)
renders the following error: Error in is(x, "GridTopology") :
trying to get slot "grid" from an object (class "estUD") that is not an S4 object
I am sorry I do not know how to provide a reproducible example for such an example as the data is quite complex. I hope that a general code exists.
Any pointers appreciated!
> str(re)
List of 1
$ ud:Formal class 'estUD' [package "adehabitatHR"] with 9 slots
.. ..# h :List of 2
.. .. ..$ h : num 0
.. .. ..$ meth: chr "specified"
.. ..# vol : logi FALSE
.. ..# data :'data.frame': 4400000 obs. of 1 variable:
.. .. ..$ ud: num [1:4400000] 0 0 0 0 0 0 0 0 0 0 ...
.. ..# coords.nrs : num(0)
.. ..# grid :Formal class 'GridTopology' [package "sp"] with 3 slots
.. .. .. ..# cellcentre.offset: Named num [1:2] -70 -60
.. .. .. .. ..- attr(*, "names")= chr [1:2] "Var2" "Var1"
.. .. .. ..# cellsize : Named num [1:2] 0.01 0.01
.. .. .. .. ..- attr(*, "names")= chr [1:2] "Var2" "Var1"
.. .. .. ..# cells.dim : Named int [1:2] 2000 2200
.. .. .. .. ..- attr(*, "names")= chr [1:2] "Var2" "Var1"
.. ..# grid.index : int [1:4400000] 1 2 3 4 5 6 7 8 9 10 ...
.. ..# coords : num [1:4400000, 1:2] -70 -70 -70 -70 -70 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:2] "Var2" "Var1"
.. ..# bbox : num [1:2, 1:2] -70 -60 -50 -38
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:2] "Var2" "Var1"
.. .. .. ..$ : chr [1:2] "min" "max"
.. ..# proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. .. ..# projargs: chr "+proj=longlat +datum=WGS84 +no_defs"
- attr(*, "class")= chr "estUDm"
my code for kerneloverlaphr(), which works with the outputs stright from kernelUD() (estUD-class) is as follows:
NWI15b, BCI15b and BCI15i are my individual animals
library(adehabitatHR)
# convert list of KDEs (estUDs) to class estUDm
tot <- list(NWI15b=NWI15b, BCI15b=BCI15b, BCI15i=BCI15i)
class(tot) <- "estUDm"
#calculate overlap using kerneloverlaphr
kerneloverlaphr(tot, method = c("BA"), percent = 95, conditional = FALSE)
dput(head(as.data.frame.estUD(re), n = 10))
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'head': no method or default for coercing “estUDm” to “SpatialPixelsDataFrame”
> unique(re#data$ud)
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'unique': trying to get slot "data" from an object (class "estUDm") that is not an S4 object
structure of object fed to kernelUD
str(track_sp)
Formal class 'SpatialPoints' [package "sp"] with 3 slots
..# coords : num [1:9790, 1:2] -59.2 -59.2 -59.2 -59.2 -59.2 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:9790] "1" "2" "3" "4" ...
.. .. ..$ : chr [1:2] "x" "y"
..# bbox : num [1:2, 1:2] -65.8 -55.6 -56.1 -52.2
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x" "y"
.. .. ..$ : chr [1:2] "min" "max"
..# proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..# projargs: chr "+proj=longlat +datum=WGS84 +no_defs"
Where we have
re <- lapply(1:ncol(udspdf), function(i) {
so <- new("estUD", udspdf[,i])
so#h <- list(h=0, meth="specified") # specify dummy h values: they are only required to recreate the estUDm
so#vol <- FALSE
return(so)
})
> names(re) <- names(udspdf)
> class(re) <- "estUDm"
> image(re)
We then need to simply add this:
colonyA <- re[[1]]
#and then do the same after running the code for the next colony
colonyB<- re[[1]]

Interpreting the PCA axis Dim1 and Dim2 from CLARA plot results directly

I had a large dataset that contains more than 300,000 rows/observations and 22 variables. I used the CLARA method for the clustering and plotted the results using fviz_cluster. Using the silhouette method, I got 10 as my number of clusters and from there I applied it to my CLARA algorithm.
clara.res <- clara(df, 10, samples = 50,trace = 1,sampsize = 1000, pamLike = TRUE)
str(clara.res)
List of 10
$ sample : chr [1:1000] "100046" "100303" "10052" "100727" ...
$ medoids : num [1:10, 1:22] 0.925 0.125 0.701 0 0 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:10] "193751" "137853" "229261" "257462" ...
.. ..$ : chr [1:22] "COD" "DMW" "HER" "SPR" ...
$ i.med : int [1:10] 104171 42062 143627 174961 300065 13836 192832 207079 185241 228575
$ clustering: Named int [1:302251] 1 1 1 2 3 4 5 3 3 3 ...
..- attr(*, "names")= chr [1:302251] "1" "10" "100" "1000" ...
$ objective : num 0.37
$ clusinfo : num [1:10, 1:4] 71811 40181 46271 10155 31309 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr [1:4] "size" "max_diss" "av_diss" "isolation"
$ diss : 'dissimilarity' num [1:499500] 1.392 2.192 0.937 2.157 1.643 ...
..- attr(*, "Size")= int 1000
..- attr(*, "Metric")= chr "euclidean"
..- attr(*, "Labels")= chr [1:1000] "100046" "100303" "10052" "100727" ...
$ call : language clara(x = df, k = 10, samples = 50, sampsize = 1000, trace = 1, pamLike = TRUE)
$ silinfo :List of 3
..$ widths : num [1:1000, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:1000] "83395" "181310" "34452" "42991" ...
.. .. ..$ : chr [1:3] "cluster" "neighbor" "sil_width"
..$ clus.avg.widths: num [1:10] 0.645 0.408 0.487 0.513 0.839 ...
..$ avg.width : num 0.612
$ data : num [1:302251, 1:22] 1 1 1 0.366 0.35 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:302251] "1" "10" "100" "1000" ...
.. ..$ : chr [1:22] "COD" "DMW" "HER" "SPR" ...
- attr(*, "class")= chr [1:2] "clara" "partition"
For the plot:
fviz_cluster(clara.res,
palette = c(
"#004c6d",
"#00a1c1",
"#ffc334",
"#78ab63",
"#00ffff",
"#00cfe3",
"#6efa75",
"#cc0089",
"#ff9509",
"#ffb6de"
), # color palette
ellipse.type = "t",geom = "point",show.clust.cent = TRUE,repel = TRUE,pointsize = 0.5,
ggtheme = theme_classic()
)+ xlim(-7, 3) + ylim (-5, 4) + labs(title = "Plot of clusters")
The result:
I reckoned that this cluster plot is based on PCA and have been trying to figure out which variables in my original data were chosen as Dim1 and Dim2 or what these x and y-axis represent. Can somebody help me how to find out these Dim1 and Dim2 and eigenvalues/variance of the whole Dim that exist without running PCA separately?
I saw there are some other functions/packages for PCA such as get_eigenvalue in factoextra and FactomineR, but it seemed that will require me to use the PCA algorithm from the beginning? How can I integrate it directly with my CLARA results?
Also, my Dim1 only consists of 12.3% and Dim2 8.8%, does it mean that these variables are not representative enough or? considering that I would have 22 dimensions in total (from my 22 variables), I think it's alright, no? I am not sure how these percentages of Dim1 and Dim2 affect my cluster results. I was thinking to do the screeplot from my CLARA results but I also can't figure it out.
I'd appreciate any insights.

R: How to "extract" the p-values of a Dunnetts-test (Post-Hoc after ANOVA)

I am a biochemist working with R as a non-professional and get into a problem now. I have a dataframe and I want to compare my different treatment groups and the positive control with a medium control. The statistical test I want to use is an anova followed by a Dunnetts test. I used the multcomp- and the DescTools-package for this and I get there with this code
Particle <- factor(c("Medium", "PosCon", "Trt1", "Trt2", "Trt3", "Medium", "PosCon", "Trt1", "Trt2", "Trt3", "Medium", "PosCon", "Trt1", "Trt2", "Trt3"))
Values <- c(1.0, 263.0, 3.1, 1.2, 0.9, 1.0, 244.0, 2.4, 1.6, 1.1, 1.0, 255.0, 3.8, 2.0, 0.8)
myDataframe <- data.frame(Particle, Values)
str(myDataframe)
a1 <- aov(Values ~ Particle, data= myDataframe)
summary(a1)
#Output
# Df Sum Sq Mean Sq F value Pr(>F)
#Particle 4 152832 38208 2084 1.48e-14 ***
#Residuals 10 183 18
#---
#Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
myDataframe.dunnett <- glht(a1, linfct = mcp(Particle= "Dunnett"))
myDataframe.dunnett
summary(myDataframe.dunnett)
# Output:
# Simultaneous Tests for General Linear Hypotheses
#
#Multiple Comparisons of Means: Dunnett Contrasts
#
#
#Fit: aov(formula = Values ~ Particle, data = myDataframe)
#
#Linear Hypotheses:
# Estimate Std. Error t value Pr(>|t|)
#PosCon - Medium == 0 253.00000 3.49616 72.365 <0.001 ***
#Trt1 - Medium == 0 2.10000 3.49616 0.601 0.930
#Trt2 - Medium == 0 0.60000 3.49616 0.172 0.999
#Trt3 - Medium == 0 -0.06667 3.49616 -0.019 1.000
#---
#Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#(Adjusted p values reported -- single-step method)
Now I want to get the p-values extracted (or Pr(>|t|)) and I want to get them as a four-digit number (three would also work). I used str(summary(myDataframe.dunnett)) and names(summary(myDataframe.dunnett)) to get to know what to extract, but when I extract it, it is a no digit-number as this:
str(summary(myDataframe.dunnett))
names(summary(myDataframe.dunnett)) #just to get to know the names
x <- summary(myDataframe.dunnett)$test$pvalues
x
[1] 0 1 1 1
attr(,"error")
[1] 0.0001612462
Does anyone know what that is or knows a better way to kind of "extract" the significance levels after an Anova and Dunnetts test into a vector? I need those to convert them into the significance-stars above a plot.
I have the feeling that this might help, but I could not figure out how to modify it for my data:
Thanks for your help!
If you look at the structure of the summary object you can see that the values you are hoping to extract are in a list element named test
str( summary(myDataframe.dunnett) )
List of 10
$ model :List of 13
..$ coefficients : Named num [1:5] 1 253 2.1 0.6 -0.0667
.. ..- attr(*, "names")= chr [1:5] "(Intercept)" "ParticlePosCon" "ParticleTrt1" "ParticleTrt2" ...
..$ residuals : Named num [1:15] -1.48e-15 9.00 1.92e-15 -4.00e-01 -3.33e-02 ...
.. ..- attr(*, "names")= chr [1:15] "1" "2" "3" "4" ...
..$ effects : Named num [1:15] -201.8857 -390.926 -2.8833 -0.8957 0.0816 ...
.. ..- attr(*, "names")= chr [1:15] "(Intercept)" "ParticlePosCon" "ParticleTrt1" "ParticleTrt2" ...
..$ rank : int 5
..$ fitted.values: Named num [1:15] 1 254 3.1 1.6 0.933 ...
.. ..- attr(*, "names")= chr [1:15] "1" "2" "3" "4" ...
..$ assign : int [1:5] 0 1 1 1 1
..$ qr :List of 5
.. ..$ qr : num [1:15, 1:5] -3.873 0.258 0.258 0.258 0.258 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:15] "1" "2" "3" "4" ...
.. .. .. ..$ : chr [1:5] "(Intercept)" "ParticlePosCon" "ParticleTrt1" "ParticleTrt2" ...
.. .. ..- attr(*, "assign")= int [1:5] 0 1 1 1 1
.. .. ..- attr(*, "contrasts")=List of 1
.. .. .. ..$ Particle: chr "contr.treatment"
.. ..$ qraux: num [1:5] 1.26 1.54 1.54 1.53 1.52
.. ..$ pivot: int [1:5] 1 2 3 4 5
.. ..$ tol : num 1e-07
.. ..$ rank : int 5
.. ..- attr(*, "class")= chr "qr"
..$ df.residual : int 10
..$ contrasts :List of 1
.. ..$ Particle: chr "contr.treatment"
..$ xlevels :List of 1
.. ..$ Particle: chr [1:5] "Medium" "PosCon" "Trt1" "Trt2" ...
..$ call : language aov(formula = Values ~ Particle, data = myDataframe)
..$ terms :Classes 'terms', 'formula' language Values ~ Particle
.. .. ..- attr(*, "variables")= language list(Values, Particle)
.. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:2] "Values" "Particle"
.. .. .. .. ..$ : chr "Particle"
.. .. ..- attr(*, "term.labels")= chr "Particle"
.. .. ..- attr(*, "order")= int 1
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "response")= int 1
.. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. ..- attr(*, "predvars")= language list(Values, Particle)
.. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "factor"
.. .. .. ..- attr(*, "names")= chr [1:2] "Values" "Particle"
..$ model :'data.frame': 15 obs. of 2 variables:
.. ..$ Values : num [1:15] 1 263 3.1 1.2 0.9 1 244 2.4 1.6 1.1 ...
.. ..$ Particle: Factor w/ 5 levels "Medium","PosCon",..: 1 2 3 4 5 1 2 3 4 5 ...
.. ..- attr(*, "terms")=Classes 'terms', 'formula' language Values ~ Particle
.. .. .. ..- attr(*, "variables")= language list(Values, Particle)
.. .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. ..$ : chr [1:2] "Values" "Particle"
.. .. .. .. .. ..$ : chr "Particle"
.. .. .. ..- attr(*, "term.labels")= chr "Particle"
.. .. .. ..- attr(*, "order")= int 1
.. .. .. ..- attr(*, "intercept")= int 1
.. .. .. ..- attr(*, "response")= int 1
.. .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. .. ..- attr(*, "predvars")= language list(Values, Particle)
.. .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "factor"
.. .. .. .. ..- attr(*, "names")= chr [1:2] "Values" "Particle"
..- attr(*, "class")= chr [1:2] "aov" "lm"
$ linfct : num [1:4, 1:5] 0 0 0 0 1 0 0 0 0 1 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : Named chr [1:4] "PosCon - Medium" "Trt1 - Medium" "Trt2 - Medium" "Trt3 - Medium"
.. .. ..- attr(*, "names")= chr [1:4] "Particle1" "Particle2" "Particle3" "Particle4"
.. ..$ : chr [1:5] "(Intercept)" "ParticlePosCon" "ParticleTrt1" "ParticleTrt2" ...
..- attr(*, "type")= chr "Dunnett"
$ rhs : num [1:4] 0 0 0 0
$ coef : Named num [1:5] 1 253 2.1 0.6 -0.0667
..- attr(*, "names")= chr [1:5] "(Intercept)" "ParticlePosCon" "ParticleTrt1" "ParticleTrt2" ...
$ vcov : num [1:5, 1:5] 6.11 -6.11 -6.11 -6.11 -6.11 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:5] "(Intercept)" "ParticlePosCon" "ParticleTrt1" "ParticleTrt2" ...
.. ..$ : chr [1:5] "(Intercept)" "ParticlePosCon" "ParticleTrt1" "ParticleTrt2" ...
$ df : int 10
$ alternative: chr "two.sided"
$ type : chr "Dunnett"
$ focus : chr "Particle"
$ test :List of 7
..$ pfunction :function (type = c("univariate", "adjusted", p.adjust.methods), ...)
..$ qfunction :function (conf.level, adjusted = TRUE, ...)
..$ coefficients: Named num [1:4] 253 2.1 0.6 -0.0667
.. ..- attr(*, "names")= chr [1:4] "PosCon - Medium" "Trt1 - Medium" "Trt2 - Medium" "Trt3 - Medium"
..$ sigma : Named num [1:4] 3.5 3.5 3.5 3.5
.. ..- attr(*, "names")= chr [1:4] "PosCon - Medium" "Trt1 - Medium" "Trt2 - Medium" "Trt3 - Medium"
..$ tstat : Named num [1:4] 72.3652 0.6007 0.1716 -0.0191
.. ..- attr(*, "names")= chr [1:4] "PosCon - Medium" "Trt1 - Medium" "Trt2 - Medium" "Trt3 - Medium"
..$ pvalues : num [1:4] 0 0.93 0.999 1
.. ..- attr(*, "error")= num 0.000305
..$ type : chr "single-step"
..- attr(*, "class")= chr "mtest"
- attr(*, "class")= chr [1:2] "summary.glht" "glht"
... and that the test list is a rather complicated list in its own right, ...
> ( summary(myDataframe.dunnett)$test )
$pfunction
function (type = c("univariate", "adjusted", p.adjust.methods),
...)
{
type <- match.arg(type)
pfct <- function(q) {
switch(object$alternative, two.sided = {
low <- rep(-abs(q), dim)
upp <- rep(abs(q), dim)
}, less = {
low <- rep(q, dim)
upp <- rep(Inf, dim)
}, greater = {
low <- rep(-Inf, dim)
upp <- rep(q, dim)
})
pmvt(lower = low, upper = upp, df = df, corr = cr, ...)
}
switch(object$alternative, two.sided = {
if (df > 0) pvals <- 2 * (1 - pt(abs(tstat), df)) else pvals <- 2 *
(1 - pnorm(abs(tstat)))
}, less = {
if (df > 0) pvals <- pt(tstat, df) else pvals <- pnorm(tstat)
}, greater = {
if (df > 0) pvals <- 1 - pt(tstat, df) else pvals <- 1 -
pnorm(tstat)
})
if (type == "univariate")
return(pvals)
if (type == "adjusted") {
ret <- numeric(length(tstat))
error <- 0
for (i in 1:length(tstat)) {
tmp <- pfct(tstat[i])
if (attr(tmp, "msg") != "Normal Completion" && length(grep("^univariate",
attr(tmp, "msg"))) == 0)
warning(attr(tmp, "msg"))
if (error < attr(tmp, "error"))
error <- attr(tmp, "error")
ret[i] <- tmp
}
ret <- 1 - ret
attr(ret, "error") <- error
return(ret)
}
return(p.adjust(pvals, method = type))
}
<bytecode: 0x55c5419b3f18>
<environment: 0x55c540a7c100>
$qfunction
function (conf.level, adjusted = TRUE, ...)
{
tail <- switch(object$alternative, two.sided = "both.tails",
less = "lower.tail", greater = "upper.tail")
if (adjusted) {
calpha <- qmvt(conf.level, df = df, corr = cr, tail = tail,
...)
}
else {
calpha <- qmvt(conf.level, df = df, corr = matrix(1),
tail = tail, ...)
}
ret <- calpha$quantile
attr(ret, "error") <- calpha$estim.prec
return(ret)
}
<bytecode: 0x55c5419b9d20>
<environment: 0x55c540a7c100>
$coefficients
PosCon - Medium Trt1 - Medium Trt2 - Medium Trt3 - Medium
253.00000000 2.10000000 0.60000000 -0.06666667
$sigma
PosCon - Medium Trt1 - Medium Trt2 - Medium Trt3 - Medium
3.496157 3.496157 3.496157 3.496157
$tstat
PosCon - Medium Trt1 - Medium Trt2 - Medium Trt3 - Medium
72.36517911 0.60065959 0.17161703 -0.01906856
$pvalues
[1] 0.0000000 0.9298994 0.9992779 0.9999999
attr(,"error")
[1] 0.0001864546
$type
[1] "single-step"
attr(,"class")
[1] "mtest"
... and that the p-values are in a sublist of that list in an element named pvalues:
> ( summary(myDataframe.dunnett)$test$pvalues )
[1] 0.0000000 0.9299389 0.9992772 0.9999999
attr(,"error")
[1] 0.0001414288
... or with the DescTools function DunnetTest
(z <- DunnettTest(formula(a1$call)))
## Dunnett's test for comparing several treatments with a control :
## 95% family-wise confidence level
##
## $Medium
## diff lwr.ci upr.ci pval
## PosCon-Medium 253.00000000 242.883587 263.11641 <2e-16 ***
## Trt1-Medium 2.10000000 -8.016413 12.21641 0.9299
## Trt2-Medium 0.60000000 -9.516413 10.71641 0.9993
## Trt3-Medium -0.06666667 -10.183079 10.04975 1.0000
##
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
z$Medium[,"pval"]
## PosCon-Medium Trt1-Medium Trt2-Medium Trt3-Medium
## 0.0000000 0.9298875 0.9992763 0.9999999

Control the structure of a nested list when apply FUN returns list or NA

I have a vector v and matrix m and use apply to extract a subset of results from cor.test function (correlations between v and m columns).
set.seed(1)
m <- matrix(runif(12), nrow = 3)
v <- 3:1
res <- apply(m, 2, function(x) {
cor.test(x, v, method = 'spearman', exact = F)[c(1,3,4)]
})
This is a nested list of list with length equal to the number of columns in m - and in the structure I would like as output (2-level list).
> str(res)
List of 4
$ :List of 3
..$ statistic: Named num 8
.. ..- attr(*, "names")= chr "S"
..$ p.value : num 0
..$ estimate : Named num -1
.. ..- attr(*, "names")= chr "rho"
$ :List of 3
..$ statistic: Named num 2
.. ..- attr(*, "names")= chr "S"
..$ p.value : num 0.667
..$ estimate : Named num 0.5
.. ..- attr(*, "names")= chr "rho"
$ :List of 3
..$ statistic: Named num 0
.. ..- attr(*, "names")= chr "S"
..$ p.value : num 0
..$ estimate : Named num 1
.. ..- attr(*, "names")= chr "rho"
$ :List of 3
..$ statistic: Named num 6
.. ..- attr(*, "names")= chr "S"
..$ p.value : num 0.667
..$ estimate : Named num -0.5
.. ..- attr(*, "names")= chr "rho"
I want to filter each cor.test result, say the p.value, within the apply loop and return NA to indicate filtered results (retaining the length of the result, here four).
res <- apply(m, 2, function(x) {
tmp <- cor.test(x, v, method = 'spearman', exact = F)[c(1,3,4)]
ifelse(tmp$p.value < 0.1, list(tmp), NA)
})
My problem is that we now get a 3-level list structure
res2 <- apply(m, 2, function(x) {
tmp <- cor.test(x, v, method = 'spearman', exact = F)[c(1,3,4)]
ifelse(tmp$p.value < 0.1, list(tmp), NA)
})
> str(res2)
List of 4
$ :List of 1
..$ :List of 3
.. ..$ statistic: Named num 8
.. .. ..- attr(*, "names")= chr "S"
.. ..$ p.value : num 0
.. ..$ estimate : Named num -1
.. .. ..- attr(*, "names")= chr "rho"
$ : logi NA
$ :List of 1
..$ :List of 3
.. ..$ statistic: Named num 0
.. .. ..- attr(*, "names")= chr "S"
.. ..$ p.value : num 0
.. ..$ estimate : Named num 1
.. .. ..- attr(*, "names")= chr "rho"
$ : logi NA
Only if the first result from apply is NA the result structure is like desired, obviously since apply then can fit unfiltered result to the structure.
res3 <- apply(m, 2, function(x) {
tmp <- cor.test(x, v, method = 'spearman', exact = F)[c(1,3,4)]
ifelse(tmp$p.value > 0.1, list(tmp), NA) #'invert' the test
})
>res3
List of 4
$ : logi NA
$ :List of 3
..$ statistic: Named num 2
.. ..- attr(*, "names")= chr "S"
..$ p.value : num 0.667
..$ estimate : Named num 0.5
.. ..- attr(*, "names")= chr "rho"
$ : logi NA
$ :List of 3
..$ statistic: Named num 6
.. ..- attr(*, "names")= chr "S"
..$ p.value : num 0.667
..$ estimate : Named num -0.5
.. ..- attr(*, "names")= chr "rho"
I have tried to return ifelse(tmp$p.value < 0.1, tmp, NA) and ifelse(tmp$p.value < 0.1, list(tmp), list(NA)) in vain.
The only solution I found is to assign NA outside the apply:
res4 <- apply(m, 2, function(x) {
cor.test(x, v, method = 'spearman', exact = F)[c(1,3,4)]
})
res4[sapply(res4, "[[", 2) > 0.1] <- NA
Clearly, I miss something about the inner workings of apply.
Your issue isn't with apply but with ifelse. If you use if () {} else {} instead, it works the way you intend
res3 <- apply(m, 2, function(x) {
tmp <- cor.test(x, v, method = 'spearman', exact = F)[c(1,3,4)]
if (tmp$p.value < 0.1) { return(tmp) } else { return(NA) }
})
str(res3)
# List of 4
# $ :List of 3
# ..$ statistic: Named num 8
# .. ..- attr(*, "names")= chr "S"
# ..$ p.value : num 0
# ..$ estimate : Named num -1
# .. ..- attr(*, "names")= chr "rho"
# $ : logi NA
# $ :List of 3
# ..$ statistic: Named num 0
# .. ..- attr(*, "names")= chr "S"
# ..$ p.value : num 0
# ..$ estimate : Named num 1
# .. ..- attr(*, "names")= chr "rho"
# $ : logi NA
Note documentation from ifelse
ifelse returns a value with the same shape as test

Plotting realisations of random Gaussian fields using RandomFields package results in blank graph. Why?

For some reason when I try to using the plot() function to visualise the output of the RFsimulate() function in the RandomFields package, the output is always an empty plot.
I am just using the example code included in the help file:
## first let us look at the list of implemented models
RFgetModelNames(type="positive definite", domain="single variable",
iso="isotropic")
## our choice is the exponential model;
## the model includes nugget effect and the mean:
model <- RMexp(var=5, scale=10) + # with variance 4 and scale 10
RMnugget(var=1) + # nugget
RMtrend(mean=0.5) # and mean
## define the locations:
from <- 0
to <- 20
x.seq <- seq(from, to, length=200)
y.seq <- seq(from, to, length=200)
simu <- RFsimulate(model=model, x=x.seq, y=y.seq)
str(simu)
Which gives:
Formal class 'RFspatialGridDataFrame' [package ""] with 5 slots
..# .RFparams :List of 5
.. ..$ n : num 1
.. ..$ vdim : int 1
.. ..$ T : num(0)
.. ..$ coordunits: NULL
.. ..$ varunits : NULL
..# data :'data.frame': 441 obs. of 1 variable:
.. ..$ variable1: num [1:441] 4.511 2.653 3.951 0.771 2.718 ...
..# grid :Formal class 'GridTopology' [package "sp"] with 3 slots
.. .. ..# cellcentre.offset: Named num [1:2] 0 0
.. .. .. ..- attr(*, "names")= chr [1:2] "coords.x1" "coords.x2"
.. .. ..# cellsize : Named num [1:2] 1 1
.. .. .. ..- attr(*, "names")= chr [1:2] "coords.x1" "coords.x2"
.. .. ..# cells.dim : int [1:2] 21 21
..# bbox : num [1:2, 1:2] -0.5 -0.5 20.5 20.5
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
.. .. ..$ : chr [1:2] "min" "max"
..# proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..# projargs: chr NA
... so data has been simulated, but when I call
plot(simu)
I end up with something like this:
e.g. Empty plot
Can anyone tell what going on here?!
I would coerce the object back to an sp SpatialGridDataFrame and plot that, as RandomFields creates a wrapper around this S4 class:
sgdf = sp::SpatialGridDataFrame(simu#grid, simu#data, simu#proj4string)
sp::plot(sgdf)
Also, you can coerce to matrix and plot using the standard graphics library:
graphics::image(as.matrix(simu))
The strange thing is that converting it to a SpatialGridDataFrame requires a flip and transpose before plotting:
graphics::image(t(apply(as.matrix(sgdf), 1, rev)))
Apparently, they are a bit internally inconsistent. The simplest solution is to convert simu to raster and plot:
r = raster::raster(simu)
raster::plot(r)

Resources