Lab 6

pdf

School

Drexel University *

*We aren’t endorsed by this school

Course

410

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

6

Uploaded by MegaOysterMaster676

Report
11/7/23, 5 : 26 PM Lab 6 Page 1 of 6 file:///Users/mathabib/Documents/R%20files/Lab-6.html Lab 6 Mathew H 2023-11-01 library (magrittr) library (formattable) library (LearningStats) library (knitr) Exercise 1: Hamburgers: 320 Chicken Sandwiches: 280 Assumptions: 1: 2 Categorical variables 2: Assumed that sample is randomly selected and that each person chooses either hamburger or chicken sandwich and cannot do both or multiple. Sample is therefore assumed to be mutually exclusive and random. 3: 2 categories, both expected frequencies well over 5 Hypothesis: Hamburger more popular than chicken sandwhich Null Hypothesis: No sandwich is more popular than the other. Hand calculations: Expected freqs are 300 use chi squared equation to calculate values Calculated X^2 is 2.667 which matches R observed1=c(320,280) expectedprobs=c(0.5,0.5) chisq.test(x=observed1,p=expectedprobs,correct=F) ## ## Chi-squared test for given probabilities ## ## data: observed1 ## X-squared = 2.6667, df = 1, p-value = 0.1025 Conclusion:
11/7/23, 5 : 26 PM Lab 6 Page 2 of 6 file:///Users/mathabib/Documents/R%20files/Lab-6.html The P value is found to be significantly higher than the alpha value, meaning the null hypothesis can be accepted with a confidence level of 95% Exercise 2: Assumptions: 1: 11 Categorical variables (0:10 worms per eel) 2: Sample is randomly selected, events are mutually exclusive 3: 11 categories binned into 3 so expected probabilities stay above 5 wormspeel=c(0:10) wormsweight=c(17,8,7,6,4,5,3,5,4,3,4) wormsmean=weighted.mean(wormspeel,wormsweight) n=17 wormprob=dpois(0:n,wormsmean) wormprob ## [1] 2.929604e-02 1.034239e-01 1.825588e-01 2.148293e-01 1.896032e-01 ## [6] 1.338713e-01 7.876772e-02 3.972485e-02 1.753009e-02 6.876283e-03 ## [11] 2.427536e-03 7.790853e-04 2.292006e-04 6.224212e-05 1.569525e-05 ## [16] 3.693933e-06 8.150440e-07 1.692560e-07 expectedworms=sum(0:n)*wormprob #expected Frequency values observedworms=c(17,8,7,6,4,5,3,5,4,3,4) obs_adj=c(sum(observedworms[1:2]),sum(observedworms[3:4]),sum(observedworms[4:11])) obs_adj ## [1] 25 13 34 exp_adj=c(sum(wormprob[1:2]),sum(wormprob[3:4]),sum(wormprob[4:n])) exp_adj[3]=exp_adj[3]+(1-sum(exp_adj)) chisq.test(x=obs_adj,p=exp_adj,correct=F) ## ## Chi-squared test for given probabilities ## ## data: obs_adj ## X-squared = 33.48, df = 2, p-value = 5.368e-08
11/7/23, 5 : 26 PM Lab 6 Page 3 of 6 file:///Users/mathabib/Documents/R%20files/Lab-6.html Conclusion: P value is significantly less than a=0.05, so the null hypothesis is rejected with 95% confidence. Exercise 3: Assumptions: 1: 5 Categorical variables 2: Events ARE mutually exclusive 3: More than 5 expected observations in each category Hypothesis: There is a relation between the number of people in the family who have the disease and the number of families in which the disease is observed Null: There is no relation between the number of people in the family who have the disease and the number of families in which the disease is observed. numpeople=c(0:4) disfreq=c(290,35,50,75,50) disprob=c(.2,.2,.2,.2,.2) #22% rounded down to 20% so probs=1. problem says 22% is an approximation dbinom(disfreq,500,0.22) ## [1] 8.391441e-68 5.362616e-20 8.475161e-13 2.042650e-05 8.475161e-13 #note that this assigns the same liklihood to each category (0 people, 1 person, et c.) chisq.test(x=disfreq,p=disprob,correct=F) ## ## Chi-squared test for given probabilities ## ## data: disfreq ## X-squared = 459.5, df = 4, p-value < 2.2e-16 Conclusion: P value of Chi squared test is significantly below alpha, so the null hypothesis can be rejected with a confidence level of 95%. The binomial distribution is a poor model of this data as it assigns the same likelihood to each category. Exercise 4: Assumptions: 1: 4 Categorical variables
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
11/7/23, 5 : 26 PM Lab 6 Page 4 of 6 file:///Users/mathabib/Documents/R%20files/Lab-6.html 2: Events ARE mutually exclusive 3: More than 5 expected observations in each category Hypothesis: Streptomycin has an e ff ect on whether a patient dies or not Null: Streptomycin has no impact on whether a patient lives or dies R1=c(4,51) R2=c(14,38) data=matrix(c(R1,R2),nrow=2,byrow=T) chisq.test(data, correct=F) ## ## Pearson's Chi-squared test ## ## data: data ## X-squared = 7.3761, df = 1, p-value = 0.00661 Hand calc: Exp = chisq.test(data)$expected sum((data-Exp)^2/Exp) ## [1] 7.376118 Conclusion: Hand calculated value and R value match The p-value of the chi squared test was significantly below alpha, so the null hypothesis can be rejected with a confidence level of 95%. Exercise 5: Assumptions: 1: Data is categorical 2: Events ARE mutually exclusive 3: More than 5 expected observations in each category (after being put in bins) Hypothesis: Gender is associated with length of mosquitofish Null: Gender and length of mosquitofish are not associated
11/7/23, 5 : 26 PM Lab 6 Page 5 of 6 file:///Users/mathabib/Documents/R%20files/Lab-6.html mosquitofish=read.csv("/Users/mathabib/Documents/R files/Exam1/Mosquitofish(1).csv") fishlength=cut(mosquitofish$FishLength, breaks=seq(16,56,by=4)) genderlength=table(mosquitofish$Gender, fishlength) genderlength ## fishlength ## (16,20] (20,24] (24,28] (28,32] (32,36] (36,40] (40,44] (44,48] (48,52] ## F 0 9 69 271 234 112 50 32 17 ## M 91 455 276 29 0 0 0 0 0 ## fishlength ## (52,56] ## F 3 ## M 0 test=chisq.test(genderlength)$expected ## Warning in chisq.test(genderlength): Chi-squared approximation may be incorrect R1=c(genderlength[1,1:7], sum(genderlength[1,8:9])) R2=c(genderlength[2,1:7], sum(genderlength[2,8:9])) matrix=matrix(c(R1,R2),2,byrow=T) rownames(matrix)=c("females","males") matrix ## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] ## females 0 9 69 271 234 112 50 49 ## males 91 455 276 29 0 0 0 0 chisq.test(matrix)$expected ## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] ## females 43.9234 223.9611 166.5228 144.8024 112.9459 54.05957 24.13374 23.65106 ## males 47.0766 240.0389 178.4772 155.1976 121.0541 57.94043 25.86626 25.34894 chisq.test(matrix, correct=F)
11/7/23, 5 : 26 PM Lab 6 Page 6 of 6 file:///Users/mathabib/Documents/R%20files/Lab-6.html ## ## Pearson's Chi-squared test ## ## data: matrix ## X-squared = 1283.7, df = 7, p-value < 2.2e-16 Conclusion: The p-value of the chi squared test was significantly below alpha, so the null hypothesis can be rejected with a confidence level of 95%.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help