when you turn it into a factor then you have to specify the number of outcomes that’s where
the “levels” is for. R understands that this is categorical data, which is necessary when later
on making use of graphics or other analyses.
FREQUENCY TABLES AND VISUALISATION
Visualization of the information between two qualitative variables
#Frequency table
Use of function tables
Table(dsLiving$cLiving)
Table(dsLiving$fFraternity)
Table(‘living situation’ = dsLiving$fLiving, Membership = dsLiving$dFraternity)
#Use of function xtabs
It generates the same table, but operates different. Structured and layout.
Tbl <- xtabs( ~ cLiving + dFraternity, data = dsLiving)
#make a table with margins totals (as in the slides of Module 1, totals of rows and columns)
Addmargins(tbl)
,#GGplot for these tables bij sommige data neemt de x-as niet optie 1 of 2 maar een
numerieke maat, dit is fout.
Ggplot(dsLiving, aes(x = fliving)) + geom_bar(fill = “orange”, col= “black”) + xlab(“Living
conditions (cliving)”)
Ggsave(paste0(dirRslt, “Tutorial01.pdf”), width = 8, height = 6)
The dodge function makes the table put the data next to each other instead of stacked on
top of each other, which makes it easier to interpret data. Woonsituatie is stating the legend
and the colours the bars will have.
ANALYSIS OF STATISTICAL INDEPENDENCE between 2 qualitative variables (categorical data)
#Chi square test
Step 1 make a frequency table
Tbl <- table(dsLiving$cliving, dsLiving$dFraternity)
Step 2 the chi square test
Chisq.test(tbl) 1st value is observed value, degrees of freedom & p-value. The approximation
of the Chisquare test is better the larger the sample size (observed values). (rule of 5!)
However, this cannot be checked by the frequency tables because these are observed and
not the expected frequencies.
Step 3 extract information from object. To check the expected frequencies
RsltChisq <- chisq.test(tbl)
Str(rsltChisq)
With this you can already see the observed and expected values
rsltChisq$statistic observed value of the statistic
rsltChisq$parameter degrees of freedom
rsltChisq$p.value p-value
Step 6 re-make the frequency table
Tbl <- table(dsLiving.tmp$cLiving, dsLiving$dFraternity)
Step 7 Find the chi-square test results
Chisq.test(tbl)
The warning message will not show anymore.
OPTION 2 – combining rows with sparse outcomes (to leave out <5)
Step 1 – copy data to temporary data frame
DsLiving.tmp <- dsLiving[c(“cLiving”, “dFraternity”)]
Step 2- adjust the value
dsLiving.tmp$cLiving[dsLiving.tmp$cLiving==5] <-4 combining outcome 5 with outcome 4
Step 3 – remake the frequency table
Tbl <- table(dsLiving.tmp$cLiving, dsLiving.tmp$dFraternity)
Step 4 – Find the chisquare test results
Chisq.test(tbl)
Also no warning message.
#if the expected frequencies are falling short of the rule of 5 then we cannot use chisq and
you cannot combine rows/columns with a 2x2 table and all expected values are above 5
Yates continuity correction - contingency analysis
Step 1 make a frequency table
Tbl <- table(dsLiving$dSports, dsLiving$dFraternity)
Step 2 Find the chisq test results
Chisq.test(tbl)
Tmp <- chisq.test(tbl)
Step 3 use phi see slides for uitleg
Phi <- sqrt(tmp$statistic/sum(tbl))
R automatically applies this correction of ½ in the formula.
# 2x2 table but the expected values are still below 5
Fisher’s exact test
Step 1 make a frequency table
Tbl <- table(dsLiving$dSports, dsLiving$dFraternity)
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller jodiesilvius. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $11.16. You're not tied to anything after your purchase.