Filtering twice in crossfilter.js giving unconsistent results - crossfilter

I am processing data from a school survey. The data has sex, grade and answer attributes. Ultimately, I want to be able to show a grouped bar chart where viewers can see answers from boys and girls. To get the data I am filtering with crossfilter.js, using first a filter for boys and then a filter for girls. I might be doing things very wrong as I get unconsistent results. Probably I am missing some points about crossfilter.js's philosophy. I would like to know if my implementation is correct, or if I should use crossfilter in a different way.
Note that the question is encoded by its index (B-13)
<html>
<head>
<meta charset="UTF-8">
<title>Prueba con crossfilter</title>
<script type = "text/javascript" src = "../static/crossfilter.js"></script>
<script type = "text/javascript" src = "../static/d3.js"></script>
</head>
<body>
<script>
var data =[{"grado": 6, "Sexo": 1, "B-13": 3},
{"grado": 6, "Sexo": 2, "B-13": 3},
{"grado": 6, "Sexo": 2, "B-13": 1},
{"grado": 6, "Sexo": 1, "B-13": 3},
{"grado": 6, "Sexo": 2, "B-13": 3},
{"grado": 6, "Sexo": 2, "B-13": 5},
{"grado": 6, "Sexo": 2, "B-13": 3},
{"grado": 6, "Sexo": 1, "B-13": 3},
{"grado": 6, "Sexo": 2, "B-13": 3},
{"grado": 6, "Sexo": 2, "B-13": 5},
{"grado": 6, "Sexo": 2, "B-13": 3},
{"grado": 6, "Sexo": 2, "B-13": 3},
{"grado": 6, "Sexo": 2, "B-13": 3},
{"grado": 6, "Sexo": 2, "B-13": 3},
{"grado": 6, "Sexo": 1, "B-13": 5},
{"grado": 6, "Sexo": 2, "B-13": 3},
{"grado": 6, "Sexo": 2, "B-13": 2},
{"grado": 6, "Sexo": 2, "B-13": 2},
{"grado": 6, "Sexo": 1, "B-13": 2},
{"grado": 6, "Sexo": 1, "B-13": 1},
{"grado": 6, "Sexo": 2, "B-13": 3},
{"grado": 6, "Sexo": 1, "B-13": 1},
{"grado": 6, "Sexo": 2, "B-13": 4},
{"grado": 6, "Sexo": 2, "B-13": 4},
{"grado": 6, "Sexo": 2, "B-13": 3},
{"grado": 7, "Sexo": 1, "B-13": 2},
{"grado": 7, "Sexo": 2, "B-13": 1},
{"grado": 7, "Sexo": 2, "B-13": 2},
{"grado": 7, "Sexo": 1, "B-13": 1},
{"grado": 7, "Sexo": 1, "B-13": 5}];
// Crossfilter:
cf = crossfilter(data);
bySex = cf.dimension(function (d) { return d["Sexo"]; });
byB13 = cf.dimension(function (d) { return d["B-13"]; });
// Men
bySex.filter("1");
totalMen = cf.groupAll().value();
console.log("Total men " + totalMen);
menAnswers = byB13.group().top(Infinity);
console.log(menAnswers);
//Women
bySex.filterAll();
bySex.filter("2");
totalWomen = cf.groupAll().value();
console.log("Total women " + totalWomen);
womenAnswers = byB13.group().top(Infinity);
console.log(womenAnswers);
</script>
</body>
</html>

Related

Clean up list output within RStudio when using lapply() or sapply()

I have a function that I am using with lapply().
lapply(x, function(x))
The output looks like this:
[[1]]
[1] "att_admire"
[[2]]
[1] "att_hypocrisy"
[[3]]
[1] "att_annoyed"
[[4]]
[1] ""
[[5]]
[1] "att_respectvalues"
When I use sapply(), it looks like this:
att_admire att_hypocrisy att_annoyed att_judge
"att_admire" "att_hypocrisy" "att_annoyed" ""
att_respectvalues
"att_respectvalues"
I would like the output to look neater, like this. Only the variable names should be included, not the "" or indices.
att_admire, att_hypocrisy, att_annoyed, att_respectvalues
What can I do to modify the code I have? I looked at this question, but it focuses on how to get cleaner results in an output file, whereas I would prefer for the output to look cleaner in RStudio itself.
Below, I have included the function I am using and a sample of the data.
Libraries:
install.packages("lavaan")
library(lavaan)
Main command:
iv <- "consistent"
dv <- "behavior_solarize"
mediators <- c("att_admire", "att_hypocrisy", "att_annoyed", "att_judge", "att_respectvalues")
lapply(mediators, med_overall)
Function (using "lavaan" package):
med_overall <- function(mediator) {
mediation.model <-
paste("
# mediator
",mediator," ~ a * ",iv,"
",dv," ~ b * ",mediator,"
# direct effect
",dv," ~ c * ",iv,"
# indirect effect (a*b)
ab := a*b
# total effect
total := c + (a*b)")
fit <- sem(mediation.model, data = df, meanstructure = TRUE,
se = "boot", bootstrap = 500)
val <- parameterEstimates(fit)
ifelse(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5, 5, 5, 7, 2, 4,
3, 3, 4, 5, 7, 6, 3, 4, 3, 1, 5, 4, 5, 3, 4, 5, 4, 4, 5,
5, 4, 5, 6, 6, 2, 5, 6, 4, 6, 2, 7, 4, 4, 4, 4, 6, 1, 5,
4, 7, 4, 5, 4, 7, 5, 4, 6, 5, 3, 4, 7, 3, 4, 5, 5, 6, 6,
5, 5, 3, 4, 4, 4, 2, 3, 4, 5, 4, 7, 5, 3, 5, 7, 6, 4, 7,
7, 7, 4, 7, 4, 4, 7, 5, 5, 6, 2, 6, 5, 4, 2, 4, 5, 4, 5)), row.names = c(NA,
-693L), class = c("tbl_df", "tbl", "data.frame"))
Store the results in an object. Then exclude empty strings (where do they actually come from?) convert toString and cat it.
res <- lapply(mediators, med_overall)
cat(toString(res[res != '']))
# att_admire, att_hypocrisy, att_annoyed, att_respectvalues

Output list of variables with significant p-values from several regressions in R

I have a dataframe that looks like the following.
consistent admire trust judge
3 3 2 4
5 1 3 6
2 4 5 1
I can run the regressions I need simultaneously using the following code. In the actual dataset, there are many more than 3 variables.
variables <- c("admire", "trust", "judge")
form <- paste("consistent ~ ",variables,"")
model <- form %>%
set_names(variables) %>%
map(~lm(as.formula(.x), data = df))
map(model, summary)
This yields the output for the 3 following regressions.
summary(lm(consistent ~ admire, df))
summary(lm(consistent ~ trust, df))
summary(lm(consistent ~ judge, df))
I would like a list of the variables with significant p-values at p < 0.05. For example, if "admire" was significant and "judge" was significant, the output I am looking for would be something like:
admire, judge
Is there a way to do this that allows me to also run several regressions simultaneously? This question offers a similar answer, but I don't know how to apply it when I have several regressions.
Data:
structure(list(consistent = c(1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0,
1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1,
0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1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 5, 5, 2, 1, 2, 5, 6, 5, 5, 3, 1, 3, 1, 4, 1, 5, 2, 3, 5,
6, 4, 4, 3, 2, 4, 1, 3, 4, 3, 4, 4, 1, 5)), row.names = c(NA,
-450L), class = c("tbl_df", "tbl", "data.frame"))
To fit many many simple linear regression models, I recommend Fast pairwise simple linear regression between variables in a data frame. Hmm... looks like I need to collect those functions in an R package...
## suppose your data frame is `df`
## response variable (LHS) in column 1
## independent variable (RHS) in other columns
out <- general_paired_simpleLM(df[1], df[-1])
# LHS RHS alpha beta beta.se beta.tv beta.pv
#1 consistent admire -0.1458455 0.18754326 0.008324192 22.529906 1.040756e-75
#2 consistent trust -0.2211250 0.19565589 0.007721387 25.339475 1.531499e-88
#3 consistent judge 0.3484851 0.04824981 0.014182420 3.402086 7.287372e-04
# sig R2 F.fv F.pv
#1 0.3430602 0.53118295 507.59665 1.040756e-75
#2 0.3212008 0.58902439 642.08902 1.531499e-88
#3 0.4946862 0.02518459 11.57419 7.287372e-04
To get what you want:
with(out, RHS[beta.pv < 0.05])
#[1] "admire" "trust" "judge"

How to convert a stem and leaf plot into a data set in R?

The stem and leaf plot that I need to convert is given below-
24|9
23|
22|1
21|7
20|2, 2, 5, 5, 6, 9, 9, 9
19|0, 0, 0, 0, 0, 1, 1, 2, 4, 4, 5, 8
18|0, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 9, 9, 9
17|1, 1, 1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 9
16|0, 0, 1, 1, 1, 1, 2, 4, 5, 5, 6, 6, 8, 8, 8, 8
15|0, 1, 1, 1, 1, 1, 1, 5, 5, 5, 5, 6, 6, 6, 7, 7, 8, 9
14|0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 7, 7, 8, 9, 9
13|0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 5, 5, 6, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 9
12|1, 1, 1, 2, 2, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9
11|0, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 9, 9
10|0, 2, 3, 3, 3, 4, 4, 5, 7, 7, 8
9|0, 0, 9
8|6
Here's maybe one way. If your data looks like this
stem <- "24|9
23|
22|1
21|7
20|2, 2, 5, 5, 6, 9, 9, 9
19|0, 0, 0, 0, 0, 1, 1, 2, 4, 4, 5, 8
18|0, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 9, 9, 9
17|1, 1, 1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 9
16|0, 0, 1, 1, 1, 1, 2, 4, 5, 5, 6, 6, 8, 8, 8, 8
15|0, 1, 1, 1, 1, 1, 1, 5, 5, 5, 5, 6, 6, 6, 7, 7, 8, 9
14|0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 7, 7, 8, 9, 9
13|0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 5, 5, 6, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 9
12|1, 1, 1, 2, 2, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9
11|0, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 9, 9
10|0, 2, 3, 3, 3, 4, 4, 5, 7, 7, 8
9|0, 0, 9
8|6"
Then we can split up the rows and for each row we split by the pipe. Then we split the right side by commas and join each of those values to the value to the left of the pipe.
rows <- strsplit(stem,"\n")[[1]]
values <- unlist(lapply(strsplit(rows,"\\|"), function(x) {
end_digits <- strsplit(x[2], ", ")[[1]]
if (!all(is.na(end_digits))) {
paste0(x[1], end_digits)
} else {
NULL
}
}
))
This will return character values, but you could convert to numeric with
as.numeric(values)
Here is a different approach. Using #MrFlick's stem and rows objects:
rows <- strsplit(stem,"\n")[[1]]
rows.lst <- strsplit(rows,"\\|")
tens <- as.numeric(sapply(rows.lst, "[", 1)) * 10
ones <- sapply(strsplit(sapply(rows.lst, "[", 2), ","), as.numeric)
vals <- unlist(mapply("+", tens, ones))
vals <- vals[!is.na(vals)]

How do I return the intersection of two lists including duplicates in mathematica?

How do I find the intersection of two lists including duplicates in mathematica?
So, If I have this:
list1 = {1, 1, 3, 4, 5, 6, 6, 6, 7, 7, 10, 11, 11};
list2 = {1, 1, 4, 5, 5, 6, 6, 7, 7, 8, 11, 11, 13, 14};
I'd want it to return this:
IntersectionIncludingDuplicates[list1, list2] = {1, 1, 4, 5, 6, 6, 7, 7, 11, 11}
Thanks for any and all help!
Here's one way:
Catenate#KeyValueMap[ConstantArray]#
MapThread[Min, KeyIntersection[Counts /# {list1, list2}]]
Breaking it down:
Count how many times each element occurs in each list (Counts)
Retain only those elements which occur in both (KeyIntersection)
Take the smaller number of occurrences (MapThread, Min) and replicate the given element that many times (ConstantArray)
Edit : fixed and tested..
you might use regular Intersection , then Count , something like
ConstantArray[#, Min[Count[list1, #], Count[list2, #]]] & /#
Intersection[list1, list2] // Flatten
{1, 1, 4, 5, 6, 6, 7, 7, 11, 11}
in functional form generalised to take an arbitrary number of lists:
IntersectionIncludingDuplicates[lists__List] :=
ConstantArray[#,
Function[{v}, Min ## (Count[#, v] & /# {lists})]##] & /#
Intersection[lists] // Flatten
IntersectionIncludingDuplicates[list1, list2]
{1, 1, 4, 5, 6, 6, 7, 7, 11, 11}
Simple code to understand. Assumes input lists are sorted.
list1 = {1, 1, 3, 4, 5, 6, 6, 6, 7, 7, 10, 11, 11};
list2 = {1, 1, 4, 5, 5, 6, 6, 7, 7, 8, 11, 11, 13, 14};
IntersectionIncludingDuplicates[list1_, list2_] := Module[
{out = {}, i = j = 1},
While[i <= Length[list1] && j <= Length[list2],
If[list1[[i]] == list2[[j]],
AppendTo[out, list1[[i]]];
i++; j++,
If[list1[[i]] < list2[[j]], i++, j++]]];
out]
IntersectionIncludingDuplicates[list1, list2]
{1, 1, 4, 5, 6, 6, 7, 7, 11, 11}

Error reading dataset in R

I have problem in reading a dataset
My code :
require(igraph)
g <- graph(c(0, 1, 1, 2, 2, 0, 1, 3, 3, 4,
4, 5, 5, 3, 4, 6, 6, 7, 7, 8,
8, 6, 9, 10, 10, 11, 11, 9))
Error :
Error in graph(c(0, 1, 1, 2, 2, 0, 1, 3, 3, 4, 4, 5, 5, 3, 4, 6, 6, 7, :
At structure_generators.c:84 : Invalid (negative) vertex id, Invalid vertex id
The problem seems to be vertex of name 0
yourgraph <- c(0, 1, 1, 2, 2, 0, 1, 3, 3, 4,
4, 5, 5, 3, 4, 6, 6, 7, 7, 8,
8, 6, 9, 10, 10, 11, 11, 9)
g <- graph(yourgraph + 1)

Resources