Attribute Order [duplicate] - r

This question already has answers here:
Pattern matching using a wildcard
(6 answers)
Robust se. (vcovHC) to be shown with texreg in R
(1 answer)
Closed 6 years ago.
I do generate robust standard errors with library(sandwich)
e.g.
library(sandwich)
cov.xxx <- vcovHC(xxx, type = "HC")
rob.std.err.xxx <- sqrt(diag(cov.xxx))
Then I would like to integrate that in a Latex export presenting several models in parallel. That works "well". I use
library(texreg)
texreg(list(x, xx, xxx, xxxx),
custom.model.names = c("1", "2", "3", "4"),
custom.coef.names = c("Constant",
"A", "B", "C", "D", "E", "F", "G", "H","I", "J", "K", "L",
"Wrong Sequenced Element",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X"),
groups = list("Var1" = 1:5,
"Var2" = 6:14,
"Var3" = 15:19, "Var4" = 20:25),
sideways = T,
single.row = T,
override.se = list(c(0.1,0.1, 0.1, 0.1, 0.1),
c(0.1, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1),
c(0.1, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.1, 0.1, 0.1),
**rob.std.err.xxx #values from robust regression**
),
caption= "zzz",
return.string = TRUE, use.packages = FALSE , booktabs = TRUE, dcolumn = TRUE, digits = 3,
file = paste0(tablesdir, "xxxx.tex"))
However the order of attributes in rob.std.err.xxx does not correspond to my LM model. And I cannot figure out how to alter the SE override properly. If anybody can advise on the same procedure on p-values that would be very much appreciated!
All I would need is to alter the sequence in the rob.std.err.xxx and then export it into my target structure indicated above.
This question refers mainly to a texreg "issue". But as mentioned in the comment a solution exists.
Thanks in advance for any support.

Related

Is it possible to create bubble chart using Echarts4R

I'm trying to create a bubble chart that look like this using e_scatter.
This is what the data looks like and the state I am able to recreate now
data <- data.frame(
group = c("Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Upper", "Lower", "Lower"),
category = c("A", "B", "C", "D", "E", "F", "G", "H", "a", "b"),
size = c(0.74, 0.72, 0.68, 0.67, 0.63, 0.54, 0.53, 0.49, 0.02, 0.02),
sample_x = c(0, 0.2, 0.4, 0.6, 0.8, 0.8, 0.6, 0.4, 0.2, 0),
sample_y = c(1, 2, 3, 2, 1, -1, -2, -3, -2, -1)
)
data |>
group_by(group) |>
e_charts(sample_x) |>
e_scatter(sample_y, size)
Originally the data only have group, category and value columns
Because I don't have x and y in the data, is it possible to recreate the bubble chart using e_scatter?
Or am I using the wrong chart type for this?
(I did try by adding e_polar but I couldn't scale the size of the dot using size column)

plotly R reordering factor based on numeric variable

I need to reorder the x-axis categorical variable with a numeric variable so the plot is simpler to follow with plotly, in R.
Here is some dummy data:
agg = structure(list(catvar = c("a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o"), v1 = c(1.1, 1.3, 1,
0.8, 1.2, 1.4, 0.7, 2.5, 0.9, 2.5, 1.7, 0.9, 1.7, 1.1, 0.9),
v2 = c(0.1, 0.1, 0.1, 0, 0.1, 0.1, 0, 0.2, 0, 0.2, 0.1, 0.1,
0.1, 0.1, 0.1), v3 = c(7.3, 8.2, 6.4, 6, 7.5, 8.4, 5.8, 12.4,
6.4, 13.1, 9.3, 6.2, 9.4, 7.1, 6.3)), .Names = c("catvar",
"v1", "v2", "v3"), class = "data.frame", row.names = c(NA, -15L
))
Here is what I've been trying out. It seems recently a new feature was added to handle this but it's not working.
# the reordering is handled here:
ax <- list(
type = "category",
categoryorder = "array",
categoryarray = agg$catvar[order(agg[['v1']])],
showgrid = TRUE,
showticklabels = TRUE
)
p <- plot_ly(agg, x =~catvar,
y =~v2, name = 'v2',
type = 'scatter', mode = 'lines') %>%
add_trace(y =~v1, name = 'v1') %>%
add_trace(y =~v3, name = 'v3') %>%
layout(
xaxis = ax,
hovermode = 'x+y')
p
All I need is to re-order the x-axis (catvar) based on any of the numeric varaibles in the data.frame, these are v1, v2, v3.
You need to add to your code a command for reordering your dataset according to v1.
agg2 <- agg[order(agg[['v1']]),]
p <- plot_ly(agg2, x =~catvar,
y =~v2, name = 'v2',
type = 'scatter', mode = 'points') %>%
add_trace(y =~v1, name = 'v1') %>%
add_trace(y =~v3, name = 'v3') %>%
layout(
xaxis = ax,
hovermode = 'x+y')
p

Error in ggtexttable (ggpubr)

I'm trying to create a publication-ready table using the ggtexttable function from ggpubr. I have a data frame:
dput(df)
structure(list(feature = list("start_codon", "stop_codon", "intergenic",
"3UTR", "5UTR", "exon", "intron", "ncRNA", "pseudogene"),
observed = list(structure(1L, .Names = "start_codon"), structure(1L, .Names = "stop_codon"),
structure(418L, .Names = "intergenic"), structure(48L, .Names = "3UTR"),
structure(28L, .Names = "5UTR"), structure(223L, .Names = "exon"),
structure(578L, .Names = "intron"), structure(20L, .Names = "ncRNA"),
structure(1L, .Names = "pseudogene")), expected = list(
0.286, 0.286, 369.02, 72.461, 33.165, 257.869, 631.189,
48.491, 3.172), fc = list(3.5, 3.5, 1.1, 0.7, 0.8, 0.9,
0.9, 0.4, 0.3), test = list("enrichment", "enrichment",
"enrichment", "depletion", "depletion", "depletion",
"depletion", "depletion", "depletion"), sig = list("F",
"F", "T", "T", "F", "T", "T", "T", "F"), p_val = list(
"0.249", "0.249", "0.00186", "0.00116", "0.209", "0.00814",
"0.00237", "<1e-04", "0.175")), class = "data.frame", row.names = c(NA,
-9L), .Names = c("feature", "observed", "expected", "fc", "test",
"sig", "p_val"))
And when I try to turn this into a table:
ggtexttable(df)
I get the error:
Error in (function (label, parse = FALSE, col = "black", fontsize =
12, : unused arguments (label.feature = dots[[5]][1],
label.observed = dots[[6]][1], label.expected = dots[[7]][1],
label.fc = dots[[8]][1], label.test = dots[[9]][1], label.sig_val
= dots[[10]][1], label.p_val = dots[[11]][1])
Does anyone know what might be causing this?
This works fine:
df <- head(iris)
ggtexttable(df)
I have found the problem and solution which is going to work for you. First of all your data is not in proper format (nested list) thats why you were getting this error trying to display it. You can check what is the format of the dataset easily by pasting in your console: str(data)
Here is the solution to convert your data to data.frame:
first.step <- lapply(data, unlist)
second.step <- as.data.frame(first.step, stringsAsFactors = F)
Then you can easily use the function ggtexttable(second.step) and it displays the table with your data.

Error with using unlist, lapply and grepl in data.tables R

This question is an extension of this particular question. I have this particular data.table. I'm using data.table, mc2d, and e1071 libraries
library("data.table")
library("mc2d")
library("e1071")
col <- c("COST","TIME")
dt <- structure(
list(
ID = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),COST_PR_L = c(NA, 0.4, 0.31, 0.4, 0.5, 0.17, 1, 0.5, 0.5, 0.5),COST_PR_U = c(7.5, 2, 2.67, 1.67, 2.4,2, 1.5, 2, 2, 1.67),COST_PO_L = c(NA, 0.33, 0.25, 0.44,0.5, 0.25, 1, 0.5, 0.5, 0.5),COST_PO_U = c(3, 1.43, 3.33,1.8, 2.4, 3.6, 1.45, 2, 1.5, 1.67), TIME_PR_L = c(NA, 0.5,0.4, 0.5, 0.5, NA, 0.67, 0.5, 0.5, 0.5), TIME_PR_U = c(2,2.5, 3, 1.5, 2, NA, 1.5, 2, 1.67, 2), TIME_PO_L = c(NA,0.4, 0.25, 0.56, 0.5, NA, 0.6, 0.5, 0.5, 0.5), TIME_PO_U = c(2,2, 5, 1.67, 2.5, NA, 1.5, 2, 1.67, 2)
),.Names = c("ID","COST_PR_L", "COST_PR_U","COST_PO_L","COST_PO_U","TIME_PR_L","TIME_PR_U","TIME_PO_L","TIME_PO_U"),class = c("data.table","data.frame"),row.names = c(NA,-10L))
When I run this particular operation on it,
dt[, unlist(lapply(col, function(xx) {
y = colnames(dt)[grepl(pattern = xx, x = colnames(dt))]
vars1 = y[grepl(pattern = "PR", x = y)]
vars2 = y[grepl(pattern = "PO", x = y)]
mn = get(vars1[1])
mx = get(vars1[2])
sk1 = ifelse(mn !=0 && mx !=0,skewness(rpert(1000, min = mn , mode = 1, max= mx )),-1)
mn = get(vars2[1])
mx = get(vars2[2])
sk2 = ifelse(mn !=0 && mx !=0,skewness(rpert(1000, min = mn , mode = 1, max= mx )),-1)
return(list(sk1, sk2))
}), recursive = FALSE)
, by = "ID"]
I get the following error
Error in [.data.table(dt, , unlist(lapply(col, function(xx) { :
Column 1 of result for group 2 is type 'double' but expecting type
'logical'. Column types must be consistent for each group.
However, If I remove the unlist in the code, It seems to calculate the answer. What is unlist doing that is messing it up?

How to use radarchart {fmsb} Drawing radar chart (a.k.a. spider plot)

Im using radarchart of the library "fmsb". My problem is that I dont understand how the input has to be (see here "df" with type data frame: radarchart).
My code:
dat2 <- data.frame(c(0.6, 0.4, 0.5), c(0.5, 0.3, 0.4), c(0.4, 0.2, 0.3), c(0.7, 0.5, 0.6), c(0.9, 0.7, 0.8))
colnames(dat2) <- c("A", "B", "C", "D", "E")
radarchart(dat2, axistype=1, seg=5, plty=1, vlabels=c("A", "B", "C", "D", "E"), vlcex=1, title="(PAKR)")
My purpose is to get a line conncting the points A:0.5, B:0.4, C:0.3, D:0.6, and E:0.8. Something like specifing only a vector like in LaTeX using tkz-kiviat where you need only one vector for drawing a Spider Chart.
Thank you.
One solution is to use this data:
dat2 <- data.frame(c(100, -10, 50), c(100, -10, 40), c(100, -10, 30), c(100, -10, 60), c(100, -10, 80))

Resources