Use R dplyr/purrr To Get Chi-square Output Matrices By GroupCreate Multiple 2-dimensional Tables from Multiple Columns in R Using dplyrdata.table vs dplyr: can one do something well the other can't or does poorly?r cumsum per group in dplyrpass grouped dataframe to own function in dplyrChi-square statistic across multiple columns of a dataframe using dplyr or reshape2add results of chi square test to each rowGet `chisq.test()$p.value` for several groups using `dplyr::group_by()`Extracting unique values of repeating subset of data frameUse Dplyr::Bind_Rows and Purrr to Selectively Bind Different Dataframes In a List of DataframesChi -Square test with grouped data in dplyrdplyr group by: add null groups

Is pointing finger in meeting consider bad?

Past vs. present tense when referring to a fictional character

Was the Lonely Mountain, where Smaug lived, a volcano?

Is it ethical to cite a reviewer's papers even if they are rather irrelevant?

In The Incredibles 2, why does Screenslaver's name use a pun on something that doesn't exist in the 1950s pastiche?

DBCC SHRINKFILE on the distribution database

Can an escape pod land on Earth from orbit and not be immediately detected?

Why do the “Shtei HaLechem” not play a prominent part in the davenning for Shavuos?

What is the color associated with lukewarm?

usage of mir gefallen

Would a character with eternal youth be AL-compliant?

I sent an angry e-mail to my interviewers about a conflict at my home institution. Could this affect my application?

Why is gun control associated with the socially liberal Democratic party?

Why did Robert pick unworthy men for the White Cloaks?

My parents claim they cannot pay for my college education; what are my options?

Dedicated bike GPS computer over smartphone

Can Dive Down protect a creature against Pacifism?

typeid("") != typeid(const char*)

Is it possible to install Firefox on Ubuntu with no desktop enviroment?

Why is it bad to use your whole foot in rock climbing

New Site Design!

Can artificial satellite positions affect tides?

ISP is not hashing the password I log in with online. Should I take any action?

Getting Ready to Move my Tomato Plants Outside



Use R dplyr/purrr To Get Chi-square Output Matrices By Group


Create Multiple 2-dimensional Tables from Multiple Columns in R Using dplyrdata.table vs dplyr: can one do something well the other can't or does poorly?r cumsum per group in dplyrpass grouped dataframe to own function in dplyrChi-square statistic across multiple columns of a dataframe using dplyr or reshape2add results of chi square test to each rowGet `chisq.test()$p.value` for several groups using `dplyr::group_by()`Extracting unique values of repeating subset of data frameUse Dplyr::Bind_Rows and Purrr to Selectively Bind Different Dataframes In a List of DataframesChi -Square test with grouped data in dplyrdplyr group by: add null groups






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'd like to get chi-square output matrices (e.g., standardized residuals, expected values) by group using elements of the tidyverse. Using the mtcars data set, here's where I've started:



mtcars %>% 
dplyr::select(vs, am) %>%
table() %>%
chisq.test(.)


Which produces the chi-square test statistic. In order to get standardized residuals, for example, my only successful code is this:



mtcars %>% 
dplyr::select(vs, am) %>%
table() %>%
chisq.test(.) -> chi.out

chi.out$stdres

vs am Freq
1 0 0 0.9523038
2 1 0 -0.9523038
3 0 1 -0.9523038
4 1 1 0.9523038


Ideally, I'd like to get the observed values and the standardized residuals into a dataframe format. Something like this:



cbind(as.data.frame(chi.out$observed),as.data.frame(chi.out$stdres))

vs am Freq vs am Freq
1 0 0 12 0 0 0.9523038
2 1 0 7 1 0 -0.9523038
3 0 1 6 0 1 -0.9523038
4 1 1 7 1 1 0.9523038


Finally, I'd like to do this by group, for example over the cyl column in the mtcars data set. Seems dplyr and some some version of purrr's map with map_dfr or map_dfc would do the trick but I can't quite pull it together. Thanks in advance.










share|improve this question

















  • 2





    "I'd like to get chi-square output matrices by group" Which group? You're not grouping by any variable. Can you add your expected output for the case where you are grouping by cyl?

    – Maurits Evers
    Mar 25 at 1:32












  • Should have provided an example of what the output should look like. See Humpelstielzchen's response/solution below.

    – Joe
    Mar 25 at 18:20

















0















I'd like to get chi-square output matrices (e.g., standardized residuals, expected values) by group using elements of the tidyverse. Using the mtcars data set, here's where I've started:



mtcars %>% 
dplyr::select(vs, am) %>%
table() %>%
chisq.test(.)


Which produces the chi-square test statistic. In order to get standardized residuals, for example, my only successful code is this:



mtcars %>% 
dplyr::select(vs, am) %>%
table() %>%
chisq.test(.) -> chi.out

chi.out$stdres

vs am Freq
1 0 0 0.9523038
2 1 0 -0.9523038
3 0 1 -0.9523038
4 1 1 0.9523038


Ideally, I'd like to get the observed values and the standardized residuals into a dataframe format. Something like this:



cbind(as.data.frame(chi.out$observed),as.data.frame(chi.out$stdres))

vs am Freq vs am Freq
1 0 0 12 0 0 0.9523038
2 1 0 7 1 0 -0.9523038
3 0 1 6 0 1 -0.9523038
4 1 1 7 1 1 0.9523038


Finally, I'd like to do this by group, for example over the cyl column in the mtcars data set. Seems dplyr and some some version of purrr's map with map_dfr or map_dfc would do the trick but I can't quite pull it together. Thanks in advance.










share|improve this question

















  • 2





    "I'd like to get chi-square output matrices by group" Which group? You're not grouping by any variable. Can you add your expected output for the case where you are grouping by cyl?

    – Maurits Evers
    Mar 25 at 1:32












  • Should have provided an example of what the output should look like. See Humpelstielzchen's response/solution below.

    – Joe
    Mar 25 at 18:20













0












0








0








I'd like to get chi-square output matrices (e.g., standardized residuals, expected values) by group using elements of the tidyverse. Using the mtcars data set, here's where I've started:



mtcars %>% 
dplyr::select(vs, am) %>%
table() %>%
chisq.test(.)


Which produces the chi-square test statistic. In order to get standardized residuals, for example, my only successful code is this:



mtcars %>% 
dplyr::select(vs, am) %>%
table() %>%
chisq.test(.) -> chi.out

chi.out$stdres

vs am Freq
1 0 0 0.9523038
2 1 0 -0.9523038
3 0 1 -0.9523038
4 1 1 0.9523038


Ideally, I'd like to get the observed values and the standardized residuals into a dataframe format. Something like this:



cbind(as.data.frame(chi.out$observed),as.data.frame(chi.out$stdres))

vs am Freq vs am Freq
1 0 0 12 0 0 0.9523038
2 1 0 7 1 0 -0.9523038
3 0 1 6 0 1 -0.9523038
4 1 1 7 1 1 0.9523038


Finally, I'd like to do this by group, for example over the cyl column in the mtcars data set. Seems dplyr and some some version of purrr's map with map_dfr or map_dfc would do the trick but I can't quite pull it together. Thanks in advance.










share|improve this question














I'd like to get chi-square output matrices (e.g., standardized residuals, expected values) by group using elements of the tidyverse. Using the mtcars data set, here's where I've started:



mtcars %>% 
dplyr::select(vs, am) %>%
table() %>%
chisq.test(.)


Which produces the chi-square test statistic. In order to get standardized residuals, for example, my only successful code is this:



mtcars %>% 
dplyr::select(vs, am) %>%
table() %>%
chisq.test(.) -> chi.out

chi.out$stdres

vs am Freq
1 0 0 0.9523038
2 1 0 -0.9523038
3 0 1 -0.9523038
4 1 1 0.9523038


Ideally, I'd like to get the observed values and the standardized residuals into a dataframe format. Something like this:



cbind(as.data.frame(chi.out$observed),as.data.frame(chi.out$stdres))

vs am Freq vs am Freq
1 0 0 12 0 0 0.9523038
2 1 0 7 1 0 -0.9523038
3 0 1 6 0 1 -0.9523038
4 1 1 7 1 1 0.9523038


Finally, I'd like to do this by group, for example over the cyl column in the mtcars data set. Seems dplyr and some some version of purrr's map with map_dfr or map_dfc would do the trick but I can't quite pull it together. Thanks in advance.







r dplyr purrr






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 25 at 0:54









JoeJoe

566




566







  • 2





    "I'd like to get chi-square output matrices by group" Which group? You're not grouping by any variable. Can you add your expected output for the case where you are grouping by cyl?

    – Maurits Evers
    Mar 25 at 1:32












  • Should have provided an example of what the output should look like. See Humpelstielzchen's response/solution below.

    – Joe
    Mar 25 at 18:20












  • 2





    "I'd like to get chi-square output matrices by group" Which group? You're not grouping by any variable. Can you add your expected output for the case where you are grouping by cyl?

    – Maurits Evers
    Mar 25 at 1:32












  • Should have provided an example of what the output should look like. See Humpelstielzchen's response/solution below.

    – Joe
    Mar 25 at 18:20







2




2





"I'd like to get chi-square output matrices by group" Which group? You're not grouping by any variable. Can you add your expected output for the case where you are grouping by cyl?

– Maurits Evers
Mar 25 at 1:32






"I'd like to get chi-square output matrices by group" Which group? You're not grouping by any variable. Can you add your expected output for the case where you are grouping by cyl?

– Maurits Evers
Mar 25 at 1:32














Should have provided an example of what the output should look like. See Humpelstielzchen's response/solution below.

– Joe
Mar 25 at 18:20





Should have provided an example of what the output should look like. See Humpelstielzchen's response/solution below.

– Joe
Mar 25 at 18:20












1 Answer
1






active

oldest

votes


















1














So this is my proposal for a solution.



library(dplyr)
library(reshape2)

mtcars %>%
select(vs, am, cyl) %>%
table() %>%
apply(3, chisq.test) %>%
lapply(`[`, c(6,9)) %>%
melt() %>%
spread(key = L2, value = value) %>%
rename(cyl = L1) %>%
select(cyl, vs, am, observed, stdres) %>%
arrange(cyl)


cyl vs am observed stdres
1 4 0 0 0 -0.6422616
2 4 0 1 1 0.6422616
3 4 1 0 3 0.6422616
4 4 1 1 7 -0.6422616
5 6 0 0 0 -2.6457513
6 6 0 1 3 2.6457513
7 6 1 0 4 2.6457513
8 6 1 1 0 -2.6457513
9 8 0 0 12 NaN
10 8 0 1 2 NaN
11 8 1 0 0 NaN
12 8 1 1 0 NaN



This does a chi-square test for each group of cyl. The grouping is done implicitly in the select() statement. In the end you get the observed values and standardized residuals for every combination of cyl, vs, am. Should be applicable to any dataframe.



Hope this is what you were looking for.






share|improve this answer

























  • That works perfectly! Thanks so much. Have to load tidyr (or tidyverse) so that the spread function works. The NaN's for 8-cylinder vehicles are the result of a matrix with zeros in the marginals.

    – Joe
    Mar 25 at 18:19











  • Glad to help! Yes it wasn't able to calculate residuals this way and also gave a warning. But it was just a dummy example.

    – Humpelstielzchen
    Mar 25 at 18:23











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55330043%2fuse-r-dplyr-purrr-to-get-chi-square-output-matrices-by-group%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














So this is my proposal for a solution.



library(dplyr)
library(reshape2)

mtcars %>%
select(vs, am, cyl) %>%
table() %>%
apply(3, chisq.test) %>%
lapply(`[`, c(6,9)) %>%
melt() %>%
spread(key = L2, value = value) %>%
rename(cyl = L1) %>%
select(cyl, vs, am, observed, stdres) %>%
arrange(cyl)


cyl vs am observed stdres
1 4 0 0 0 -0.6422616
2 4 0 1 1 0.6422616
3 4 1 0 3 0.6422616
4 4 1 1 7 -0.6422616
5 6 0 0 0 -2.6457513
6 6 0 1 3 2.6457513
7 6 1 0 4 2.6457513
8 6 1 1 0 -2.6457513
9 8 0 0 12 NaN
10 8 0 1 2 NaN
11 8 1 0 0 NaN
12 8 1 1 0 NaN



This does a chi-square test for each group of cyl. The grouping is done implicitly in the select() statement. In the end you get the observed values and standardized residuals for every combination of cyl, vs, am. Should be applicable to any dataframe.



Hope this is what you were looking for.






share|improve this answer

























  • That works perfectly! Thanks so much. Have to load tidyr (or tidyverse) so that the spread function works. The NaN's for 8-cylinder vehicles are the result of a matrix with zeros in the marginals.

    – Joe
    Mar 25 at 18:19











  • Glad to help! Yes it wasn't able to calculate residuals this way and also gave a warning. But it was just a dummy example.

    – Humpelstielzchen
    Mar 25 at 18:23















1














So this is my proposal for a solution.



library(dplyr)
library(reshape2)

mtcars %>%
select(vs, am, cyl) %>%
table() %>%
apply(3, chisq.test) %>%
lapply(`[`, c(6,9)) %>%
melt() %>%
spread(key = L2, value = value) %>%
rename(cyl = L1) %>%
select(cyl, vs, am, observed, stdres) %>%
arrange(cyl)


cyl vs am observed stdres
1 4 0 0 0 -0.6422616
2 4 0 1 1 0.6422616
3 4 1 0 3 0.6422616
4 4 1 1 7 -0.6422616
5 6 0 0 0 -2.6457513
6 6 0 1 3 2.6457513
7 6 1 0 4 2.6457513
8 6 1 1 0 -2.6457513
9 8 0 0 12 NaN
10 8 0 1 2 NaN
11 8 1 0 0 NaN
12 8 1 1 0 NaN



This does a chi-square test for each group of cyl. The grouping is done implicitly in the select() statement. In the end you get the observed values and standardized residuals for every combination of cyl, vs, am. Should be applicable to any dataframe.



Hope this is what you were looking for.






share|improve this answer

























  • That works perfectly! Thanks so much. Have to load tidyr (or tidyverse) so that the spread function works. The NaN's for 8-cylinder vehicles are the result of a matrix with zeros in the marginals.

    – Joe
    Mar 25 at 18:19











  • Glad to help! Yes it wasn't able to calculate residuals this way and also gave a warning. But it was just a dummy example.

    – Humpelstielzchen
    Mar 25 at 18:23













1












1








1







So this is my proposal for a solution.



library(dplyr)
library(reshape2)

mtcars %>%
select(vs, am, cyl) %>%
table() %>%
apply(3, chisq.test) %>%
lapply(`[`, c(6,9)) %>%
melt() %>%
spread(key = L2, value = value) %>%
rename(cyl = L1) %>%
select(cyl, vs, am, observed, stdres) %>%
arrange(cyl)


cyl vs am observed stdres
1 4 0 0 0 -0.6422616
2 4 0 1 1 0.6422616
3 4 1 0 3 0.6422616
4 4 1 1 7 -0.6422616
5 6 0 0 0 -2.6457513
6 6 0 1 3 2.6457513
7 6 1 0 4 2.6457513
8 6 1 1 0 -2.6457513
9 8 0 0 12 NaN
10 8 0 1 2 NaN
11 8 1 0 0 NaN
12 8 1 1 0 NaN



This does a chi-square test for each group of cyl. The grouping is done implicitly in the select() statement. In the end you get the observed values and standardized residuals for every combination of cyl, vs, am. Should be applicable to any dataframe.



Hope this is what you were looking for.






share|improve this answer















So this is my proposal for a solution.



library(dplyr)
library(reshape2)

mtcars %>%
select(vs, am, cyl) %>%
table() %>%
apply(3, chisq.test) %>%
lapply(`[`, c(6,9)) %>%
melt() %>%
spread(key = L2, value = value) %>%
rename(cyl = L1) %>%
select(cyl, vs, am, observed, stdres) %>%
arrange(cyl)


cyl vs am observed stdres
1 4 0 0 0 -0.6422616
2 4 0 1 1 0.6422616
3 4 1 0 3 0.6422616
4 4 1 1 7 -0.6422616
5 6 0 0 0 -2.6457513
6 6 0 1 3 2.6457513
7 6 1 0 4 2.6457513
8 6 1 1 0 -2.6457513
9 8 0 0 12 NaN
10 8 0 1 2 NaN
11 8 1 0 0 NaN
12 8 1 1 0 NaN



This does a chi-square test for each group of cyl. The grouping is done implicitly in the select() statement. In the end you get the observed values and standardized residuals for every combination of cyl, vs, am. Should be applicable to any dataframe.



Hope this is what you were looking for.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 25 at 8:22

























answered Mar 25 at 8:17









HumpelstielzchenHumpelstielzchen

2,6131523




2,6131523












  • That works perfectly! Thanks so much. Have to load tidyr (or tidyverse) so that the spread function works. The NaN's for 8-cylinder vehicles are the result of a matrix with zeros in the marginals.

    – Joe
    Mar 25 at 18:19











  • Glad to help! Yes it wasn't able to calculate residuals this way and also gave a warning. But it was just a dummy example.

    – Humpelstielzchen
    Mar 25 at 18:23

















  • That works perfectly! Thanks so much. Have to load tidyr (or tidyverse) so that the spread function works. The NaN's for 8-cylinder vehicles are the result of a matrix with zeros in the marginals.

    – Joe
    Mar 25 at 18:19











  • Glad to help! Yes it wasn't able to calculate residuals this way and also gave a warning. But it was just a dummy example.

    – Humpelstielzchen
    Mar 25 at 18:23
















That works perfectly! Thanks so much. Have to load tidyr (or tidyverse) so that the spread function works. The NaN's for 8-cylinder vehicles are the result of a matrix with zeros in the marginals.

– Joe
Mar 25 at 18:19





That works perfectly! Thanks so much. Have to load tidyr (or tidyverse) so that the spread function works. The NaN's for 8-cylinder vehicles are the result of a matrix with zeros in the marginals.

– Joe
Mar 25 at 18:19













Glad to help! Yes it wasn't able to calculate residuals this way and also gave a warning. But it was just a dummy example.

– Humpelstielzchen
Mar 25 at 18:23





Glad to help! Yes it wasn't able to calculate residuals this way and also gave a warning. But it was just a dummy example.

– Humpelstielzchen
Mar 25 at 18:23



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55330043%2fuse-r-dplyr-purrr-to-get-chi-square-output-matrices-by-group%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript