Proper proportion table and chisq.test() in R





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







2















I am still very new to R (Version 1.1.383 on Mac), so I apologize if this question is basic. I have searched for a solution on the internet and on this page but could not find any.



What I want to do is to make a proportion table on which I can use chisq.test() to test if there is a difference between 4 patient groups in the distribution of proportion of patients with 5 different conditions.



The data I have contains percentage of patients with conditions (CHF, DM, S, MI and CHF) and the patients groups (green, yellow, orange and red). There are 238, 196, 158 and 20 patients in the 4 groups, respectively, and some patients may have more than one condition. Therefore, I use proportions and not actual number of patients.



Like this:



condition <- c("CHF", "DM", "S", "MI", "CHF")
green <- c(30,33,35,17,15)/238*100
yellow <- c(47,32,25,21,19)/196*100
orange <- c(48,24,27,27,25)/158*100
red <- c(10,2,4,1,6)/20*100


Where 30, 47, 48 and 10 patients in the four groups have CHF. 33, 32, 24 and 2 patients have DM and so on...



Using cbind() I get this matrix:



df <- cbind(condition, green, yellow, orange, red )
df
condition green yellow orange red #I only included 1 decimal.
[1,] "CHF" "12.6" "24.9" "30.4" "50"
[2,] "DM" "13.9" "16.3" "15.2" "10"
[3,] "S" "14.7" "12.8" "17.1" "20"
[4,] "MI" "7.1" "10.7" "17.1" "5"
[5,] "CHF" "6.3" "9.7" "15.8" "30"


It almost looks like what I want but "" makes me suspect that it is coded as characters and not a proper proportion table. Also this happens when I use chisq.test():



chisq.test(df)
Error in sum(x) : invalid 'type' (character) of argument


I really hope you can help. How do I make a proper proportion table and test difference in distribution of conditions in the four groups?










share|improve this question

























  • Unfortunately, this still bugs me. Is chisq.test() even the right statistical method for this problem? The summated proportions within each group (green, yellow, orange, red) is > 100% for some groups because som subjects have multiple conditions. Does this affect the the test? Could I only look at the counts of conditions and somehow tell the chisq.test() the total number of subjects within each group?

    – Mads Lumholdt
    Jan 31 at 15:00




















2















I am still very new to R (Version 1.1.383 on Mac), so I apologize if this question is basic. I have searched for a solution on the internet and on this page but could not find any.



What I want to do is to make a proportion table on which I can use chisq.test() to test if there is a difference between 4 patient groups in the distribution of proportion of patients with 5 different conditions.



The data I have contains percentage of patients with conditions (CHF, DM, S, MI and CHF) and the patients groups (green, yellow, orange and red). There are 238, 196, 158 and 20 patients in the 4 groups, respectively, and some patients may have more than one condition. Therefore, I use proportions and not actual number of patients.



Like this:



condition <- c("CHF", "DM", "S", "MI", "CHF")
green <- c(30,33,35,17,15)/238*100
yellow <- c(47,32,25,21,19)/196*100
orange <- c(48,24,27,27,25)/158*100
red <- c(10,2,4,1,6)/20*100


Where 30, 47, 48 and 10 patients in the four groups have CHF. 33, 32, 24 and 2 patients have DM and so on...



Using cbind() I get this matrix:



df <- cbind(condition, green, yellow, orange, red )
df
condition green yellow orange red #I only included 1 decimal.
[1,] "CHF" "12.6" "24.9" "30.4" "50"
[2,] "DM" "13.9" "16.3" "15.2" "10"
[3,] "S" "14.7" "12.8" "17.1" "20"
[4,] "MI" "7.1" "10.7" "17.1" "5"
[5,] "CHF" "6.3" "9.7" "15.8" "30"


It almost looks like what I want but "" makes me suspect that it is coded as characters and not a proper proportion table. Also this happens when I use chisq.test():



chisq.test(df)
Error in sum(x) : invalid 'type' (character) of argument


I really hope you can help. How do I make a proper proportion table and test difference in distribution of conditions in the four groups?










share|improve this question

























  • Unfortunately, this still bugs me. Is chisq.test() even the right statistical method for this problem? The summated proportions within each group (green, yellow, orange, red) is > 100% for some groups because som subjects have multiple conditions. Does this affect the the test? Could I only look at the counts of conditions and somehow tell the chisq.test() the total number of subjects within each group?

    – Mads Lumholdt
    Jan 31 at 15:00
















2












2








2


1






I am still very new to R (Version 1.1.383 on Mac), so I apologize if this question is basic. I have searched for a solution on the internet and on this page but could not find any.



What I want to do is to make a proportion table on which I can use chisq.test() to test if there is a difference between 4 patient groups in the distribution of proportion of patients with 5 different conditions.



The data I have contains percentage of patients with conditions (CHF, DM, S, MI and CHF) and the patients groups (green, yellow, orange and red). There are 238, 196, 158 and 20 patients in the 4 groups, respectively, and some patients may have more than one condition. Therefore, I use proportions and not actual number of patients.



Like this:



condition <- c("CHF", "DM", "S", "MI", "CHF")
green <- c(30,33,35,17,15)/238*100
yellow <- c(47,32,25,21,19)/196*100
orange <- c(48,24,27,27,25)/158*100
red <- c(10,2,4,1,6)/20*100


Where 30, 47, 48 and 10 patients in the four groups have CHF. 33, 32, 24 and 2 patients have DM and so on...



Using cbind() I get this matrix:



df <- cbind(condition, green, yellow, orange, red )
df
condition green yellow orange red #I only included 1 decimal.
[1,] "CHF" "12.6" "24.9" "30.4" "50"
[2,] "DM" "13.9" "16.3" "15.2" "10"
[3,] "S" "14.7" "12.8" "17.1" "20"
[4,] "MI" "7.1" "10.7" "17.1" "5"
[5,] "CHF" "6.3" "9.7" "15.8" "30"


It almost looks like what I want but "" makes me suspect that it is coded as characters and not a proper proportion table. Also this happens when I use chisq.test():



chisq.test(df)
Error in sum(x) : invalid 'type' (character) of argument


I really hope you can help. How do I make a proper proportion table and test difference in distribution of conditions in the four groups?










share|improve this question
















I am still very new to R (Version 1.1.383 on Mac), so I apologize if this question is basic. I have searched for a solution on the internet and on this page but could not find any.



What I want to do is to make a proportion table on which I can use chisq.test() to test if there is a difference between 4 patient groups in the distribution of proportion of patients with 5 different conditions.



The data I have contains percentage of patients with conditions (CHF, DM, S, MI and CHF) and the patients groups (green, yellow, orange and red). There are 238, 196, 158 and 20 patients in the 4 groups, respectively, and some patients may have more than one condition. Therefore, I use proportions and not actual number of patients.



Like this:



condition <- c("CHF", "DM", "S", "MI", "CHF")
green <- c(30,33,35,17,15)/238*100
yellow <- c(47,32,25,21,19)/196*100
orange <- c(48,24,27,27,25)/158*100
red <- c(10,2,4,1,6)/20*100


Where 30, 47, 48 and 10 patients in the four groups have CHF. 33, 32, 24 and 2 patients have DM and so on...



Using cbind() I get this matrix:



df <- cbind(condition, green, yellow, orange, red )
df
condition green yellow orange red #I only included 1 decimal.
[1,] "CHF" "12.6" "24.9" "30.4" "50"
[2,] "DM" "13.9" "16.3" "15.2" "10"
[3,] "S" "14.7" "12.8" "17.1" "20"
[4,] "MI" "7.1" "10.7" "17.1" "5"
[5,] "CHF" "6.3" "9.7" "15.8" "30"


It almost looks like what I want but "" makes me suspect that it is coded as characters and not a proper proportion table. Also this happens when I use chisq.test():



chisq.test(df)
Error in sum(x) : invalid 'type' (character) of argument


I really hope you can help. How do I make a proper proportion table and test difference in distribution of conditions in the four groups?







r rstudio chi-squared






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 22:41









Brian Tompsett - 汤莱恩

4,2571339103




4,2571339103










asked Nov 23 '18 at 17:22









Mads LumholdtMads Lumholdt

134




134













  • Unfortunately, this still bugs me. Is chisq.test() even the right statistical method for this problem? The summated proportions within each group (green, yellow, orange, red) is > 100% for some groups because som subjects have multiple conditions. Does this affect the the test? Could I only look at the counts of conditions and somehow tell the chisq.test() the total number of subjects within each group?

    – Mads Lumholdt
    Jan 31 at 15:00





















  • Unfortunately, this still bugs me. Is chisq.test() even the right statistical method for this problem? The summated proportions within each group (green, yellow, orange, red) is > 100% for some groups because som subjects have multiple conditions. Does this affect the the test? Could I only look at the counts of conditions and somehow tell the chisq.test() the total number of subjects within each group?

    – Mads Lumholdt
    Jan 31 at 15:00



















Unfortunately, this still bugs me. Is chisq.test() even the right statistical method for this problem? The summated proportions within each group (green, yellow, orange, red) is > 100% for some groups because som subjects have multiple conditions. Does this affect the the test? Could I only look at the counts of conditions and somehow tell the chisq.test() the total number of subjects within each group?

– Mads Lumholdt
Jan 31 at 15:00







Unfortunately, this still bugs me. Is chisq.test() even the right statistical method for this problem? The summated proportions within each group (green, yellow, orange, red) is > 100% for some groups because som subjects have multiple conditions. Does this affect the the test? Could I only look at the counts of conditions and somehow tell the chisq.test() the total number of subjects within each group?

– Mads Lumholdt
Jan 31 at 15:00














1 Answer
1






active

oldest

votes


















2














We created a matrix with cbind and matrix can have only single class. Having 'condition' as a column changes the numeric columns to character and this creates the issue in chisq.test.



According to ?chisq.test, the input argument 'x' can be vector or matrix, but the permitted class is numeric or factor.




x - a numeric vector or matrix. x and y can also both be factors.




So, create the matrix with only numeric vectors and keep the 'condition' as row names.



df <- cbind(green, yellow, orange, red)
row.names(df) <- condition
chisq.test(df)





share|improve this answer



















  • 1





    Oh my god. Such an elegant solution. Thank you. I have been struggling with this problem for hours now.

    – Mads Lumholdt
    Nov 23 '18 at 17:29











  • @MadsLumholdt Glad to know that the solution is useful.

    – akrun
    Nov 23 '18 at 17:30














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%2f53450777%2fproper-proportion-table-and-chisq-test-in-r%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









2














We created a matrix with cbind and matrix can have only single class. Having 'condition' as a column changes the numeric columns to character and this creates the issue in chisq.test.



According to ?chisq.test, the input argument 'x' can be vector or matrix, but the permitted class is numeric or factor.




x - a numeric vector or matrix. x and y can also both be factors.




So, create the matrix with only numeric vectors and keep the 'condition' as row names.



df <- cbind(green, yellow, orange, red)
row.names(df) <- condition
chisq.test(df)





share|improve this answer



















  • 1





    Oh my god. Such an elegant solution. Thank you. I have been struggling with this problem for hours now.

    – Mads Lumholdt
    Nov 23 '18 at 17:29











  • @MadsLumholdt Glad to know that the solution is useful.

    – akrun
    Nov 23 '18 at 17:30


















2














We created a matrix with cbind and matrix can have only single class. Having 'condition' as a column changes the numeric columns to character and this creates the issue in chisq.test.



According to ?chisq.test, the input argument 'x' can be vector or matrix, but the permitted class is numeric or factor.




x - a numeric vector or matrix. x and y can also both be factors.




So, create the matrix with only numeric vectors and keep the 'condition' as row names.



df <- cbind(green, yellow, orange, red)
row.names(df) <- condition
chisq.test(df)





share|improve this answer



















  • 1





    Oh my god. Such an elegant solution. Thank you. I have been struggling with this problem for hours now.

    – Mads Lumholdt
    Nov 23 '18 at 17:29











  • @MadsLumholdt Glad to know that the solution is useful.

    – akrun
    Nov 23 '18 at 17:30
















2












2








2







We created a matrix with cbind and matrix can have only single class. Having 'condition' as a column changes the numeric columns to character and this creates the issue in chisq.test.



According to ?chisq.test, the input argument 'x' can be vector or matrix, but the permitted class is numeric or factor.




x - a numeric vector or matrix. x and y can also both be factors.




So, create the matrix with only numeric vectors and keep the 'condition' as row names.



df <- cbind(green, yellow, orange, red)
row.names(df) <- condition
chisq.test(df)





share|improve this answer













We created a matrix with cbind and matrix can have only single class. Having 'condition' as a column changes the numeric columns to character and this creates the issue in chisq.test.



According to ?chisq.test, the input argument 'x' can be vector or matrix, but the permitted class is numeric or factor.




x - a numeric vector or matrix. x and y can also both be factors.




So, create the matrix with only numeric vectors and keep the 'condition' as row names.



df <- cbind(green, yellow, orange, red)
row.names(df) <- condition
chisq.test(df)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 17:24









akrunakrun

421k13209284




421k13209284








  • 1





    Oh my god. Such an elegant solution. Thank you. I have been struggling with this problem for hours now.

    – Mads Lumholdt
    Nov 23 '18 at 17:29











  • @MadsLumholdt Glad to know that the solution is useful.

    – akrun
    Nov 23 '18 at 17:30
















  • 1





    Oh my god. Such an elegant solution. Thank you. I have been struggling with this problem for hours now.

    – Mads Lumholdt
    Nov 23 '18 at 17:29











  • @MadsLumholdt Glad to know that the solution is useful.

    – akrun
    Nov 23 '18 at 17:30










1




1





Oh my god. Such an elegant solution. Thank you. I have been struggling with this problem for hours now.

– Mads Lumholdt
Nov 23 '18 at 17:29





Oh my god. Such an elegant solution. Thank you. I have been struggling with this problem for hours now.

– Mads Lumholdt
Nov 23 '18 at 17:29













@MadsLumholdt Glad to know that the solution is useful.

– akrun
Nov 23 '18 at 17:30







@MadsLumholdt Glad to know that the solution is useful.

– akrun
Nov 23 '18 at 17:30






















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%2f53450777%2fproper-proportion-table-and-chisq-test-in-r%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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini