Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use the following data set to answer the question (10a. and 10b.) using R

ID: 3060547 • Letter: P

Question

Please use the following data set to answer the question (10a. and 10b.) using R (please include the code):

diet.txt

10. In a follow-up study, you collect data from a third group (n = 40) who received Qnexa, another prescription weight loss drug. These data are located in the text file, “qnexa.tx ". The variables are listed in the same order. Once you've imported this file, you need to append these observations to your previous data set from question #18. Create a new data set called 58Rawss by using the rhind) function. So, use the code twodrugs,

Explanation / Answer

a.
> qnexa=read.table(file.choose(),header=T)
> qnexa
pill loss male
1 2 0.59682805 0
2 2 -0.02293131 0
3 2 -2.75249272 0
4 2 -0.47239296 0
5 2 0.26697523 0
6 2 -1.70685322 0
7 2 -2.62158842 0
8 2 -0.11662225 0
9 2 0.54087290 0
10 2 -0.94085157 0
11 2 -1.26251300 0
12 2 -0.60822416 0
13 2 1.81917969 0
14 2 0.62060486 0
15 2 0.38028512 0
16 2 -1.42789637 0
17 2 -1.36431707 0
18 2 3.45489412 0
19 2 1.99858585 0
20 2 2.32945474 0
21 2 -0.45335670 1
22 2 2.74341561 1
23 2 0.60626075 1
24 2 -0.40517390 1
25 2 2.72303751 1
26 2 0.65913939 1
27 2 2.46398699 1
28 2 -1.07950472 1
29 2 2.49188469 1
30 2 0.91295244 1
31 2 0.60067495 1
32 2 1.92228496 1
33 2 -1.13440856 1
34 2 2.07854058 1
35 2 0.05411664 1
36 2 -1.30255639 1
37 2 1.85986603 1
38 2 1.50351142 1
39 2 1.17017324 1
40 2 -0.80501868 1
> diet=read.table(file.choose(),header=T)
> diet=diet[,-1]
> diet
pill loss male
1 1 0.709540159 0
2 0 2.542530492 1
3 0 -0.214148965 0
4 0 -0.362747794 0
5 0 0.066535152 1
6 1 -0.696995161 1
7 1 -0.004208418 0
8 1 1.696906741 1
9 0 -2.136372402 0
10 0 1.347649136 1
11 0 -1.382492184 0
12 0 -1.102206247 0
13 1 1.229724657 1
14 0 -1.448639412 1
15 1 0.264262564 0
16 0 -0.602253648 1
17 0 0.391796675 1
18 0 0.794553851 0
19 1 0.908514169 0
20 0 -1.014121102 1
21 1 0.846353730 0
22 1 1.119740786 0
23 0 2.769672540 0
24 1 1.255598969 1
25 0 3.545166577 1
26 1 1.378676733 1
27 0 1.521858606 0
28 1 2.312124558 1
29 0 2.221786357 0
30 0 1.702570220 1
31 0 2.546108904 0
32 1 1.567975090 0
33 0 3.293392415 1
34 0 2.508111439 0
35 1 2.800926438 1
36 1 2.302996218 0
37 1 1.205560640 0
38 1 2.480827184 0
39 0 2.141730799 1
40 0 1.575887826 0
41 0 2.924477661 1
42 1 0.804859223 0
43 1 2.585180936 0
44 1 2.052002302 1
45 1 -1.605256848 0
46 0 -0.598490789 0
47 0 1.322472123 0
48 0 -0.220292151 0
49 0 -1.187760399 1
50 1 -0.324148252 1
51 0 -0.704780429 1
52 1 2.042973093 1
53 1 -1.197843626 1
54 1 1.363241155 0
55 0 0.049038865 1
56 1 1.123379478 0
57 1 2.093884474 0
58 0 1.107539957 0
59 0 0.508552938 1
60 1 -0.556921693 1
61 1 0.723599183 0
62 1 3.677745386 0
63 0 0.682989533 0
64 0 2.316761839 0
65 0 2.054723505 1
66 0 1.525332222 0
67 0 0.535010531 1
68 1 3.201192937 0
69 0 2.212792969 1
70 1 2.909777422 1
71 1 2.970150671 1
72 1 3.037157580 1
73 0 3.096913065 1
74 0 -1.044458606 1
75 1 3.480819693 0
76 1 3.777144016 1
77 1 2.786905660 1
78 1 3.454894124 0
79 1 1.998585854 1
80 0 2.329454742 1
> twodrugs=rbind(diet,qnexa)
> twodrugs
pill loss male
1 1 0.709540159 0
2 0 2.542530492 1
3 0 -0.214148965 0
4 0 -0.362747794 0
5 0 0.066535152 1
6 1 -0.696995161 1
7 1 -0.004208418 0
8 1 1.696906741 1
9 0 -2.136372402 0
10 0 1.347649136 1
11 0 -1.382492184 0
12 0 -1.102206247 0
13 1 1.229724657 1
14 0 -1.448639412 1
15 1 0.264262564 0
16 0 -0.602253648 1
17 0 0.391796675 1
18 0 0.794553851 0
19 1 0.908514169 0
20 0 -1.014121102 1
21 1 0.846353730 0
22 1 1.119740786 0
23 0 2.769672540 0
24 1 1.255598969 1
25 0 3.545166577 1
26 1 1.378676733 1
27 0 1.521858606 0
28 1 2.312124558 1
29 0 2.221786357 0
30 0 1.702570220 1
31 0 2.546108904 0
32 1 1.567975090 0
33 0 3.293392415 1
34 0 2.508111439 0
35 1 2.800926438 1
36 1 2.302996218 0
37 1 1.205560640 0
38 1 2.480827184 0
39 0 2.141730799 1
40 0 1.575887826 0
41 0 2.924477661 1
42 1 0.804859223 0
43 1 2.585180936 0
44 1 2.052002302 1
45 1 -1.605256848 0
46 0 -0.598490789 0
47 0 1.322472123 0
48 0 -0.220292151 0
49 0 -1.187760399 1
50 1 -0.324148252 1
51 0 -0.704780429 1
52 1 2.042973093 1
53 1 -1.197843626 1
54 1 1.363241155 0
55 0 0.049038865 1
56 1 1.123379478 0
57 1 2.093884474 0
58 0 1.107539957 0
59 0 0.508552938 1
60 1 -0.556921693 1
61 1 0.723599183 0
62 1 3.677745386 0
63 0 0.682989533 0
64 0 2.316761839 0
65 0 2.054723505 1
66 0 1.525332222 0
67 0 0.535010531 1
68 1 3.201192937 0
69 0 2.212792969 1
70 1 2.909777422 1
71 1 2.970150671 1
72 1 3.037157580 1
73 0 3.096913065 1
74 0 -1.044458606 1
75 1 3.480819693 0
76 1 3.777144016 1
77 1 2.786905660 1
78 1 3.454894124 0
79 1 1.998585854 1
80 0 2.329454742 1
81 2 0.596828052 0
82 2 -0.022931314 0
83 2 -2.752492722 0
84 2 -0.472392961 0
85 2 0.266975232 0
86 2 -1.706853224 0
87 2 -2.621588416 0
88 2 -0.116622248 0
89 2 0.540872896 0
90 2 -0.940851574 0
91 2 -1.262513002 0
92 2 -0.608224164 0
93 2 1.819179692 0
94 2 0.620604861 0
95 2 0.380285117 0
96 2 -1.427896371 0
97 2 -1.364317065 0
98 2 3.454894124 0
99 2 1.998585854 0
100 2 2.329454742 0
101 2 -0.453356697 1
102 2 2.743415611 1
103 2 0.606260752 1
104 2 -0.405173898 1
105 2 2.723037509 1
106 2 0.659139394 1
107 2 2.463986995 1
108 2 -1.079504720 1
109 2 2.491884688 1
110 2 0.912952444 1
111 2 0.600674949 1
112 2 1.922284962 1
113 2 -1.134408563 1
114 2 2.078540579 1
115 2 0.054116644 1
116 2 -1.302556394 1
117 2 1.859866030 1
118 2 1.503511425 1
119 2 1.170173235 1
120 2 -0.805018678 1

b.
> attach(twodrugs)
> pill0=loss[which(pill==0)]
> pill1=loss[which(pill==1)]
> pill2=loss[which(pill==2)]

#Checking the assumptions of ANOVA : Equality of variances among the groups
> var.test(pill1,pill2)

F test to compare two variances

data: pill1 and pill2
F = 0.77571, num df = 38, denom df = 39, p-value = 0.4354
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.4090623 1.4749593
sample estimates:
ratio of variances
0.7757103

> var.test(pill2,pill0)

F test to compare two variances

data: pill2 and pill0
F = 1.0161, num df = 39, denom df = 40, p-value = 0.9592
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.5403116 1.9157091
sample estimates:
ratio of variances
1.016088

> var.test(pill1,pill0)

F test to compare two variances

data: pill1 and pill0
F = 0.78819, num df = 38, denom df = 40, p-value = 0.463
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.4178776 1.4944944
sample estimates:
ratio of variances
0.78819
Since p-values for each of the F-test > 0.05, we accept the null hypothesis of equal variances and continue with the method of ANOVA on the data.

> weightloss=c(pill0,pill1,pill2)
> pilltype=c(rep(0,length(pill0)),rep(1,length(pill1)),rep(2,length(pill2)))
> pilltype=as.factor(pilltype)
> df=data.frame(weightloss,pilltype)
> df
weightloss pilltype
1 2.542530492 0
2 -0.214148965 0
3 -0.362747794 0
4 0.066535152 0
5 -2.136372402 0
6 1.347649136 0
7 -1.382492184 0
8 -1.102206247 0
9 -1.448639412 0
10 -0.602253648 0
11 0.391796675 0
12 0.794553851 0
13 -1.014121102 0
14 2.769672540 0
15 3.545166577 0
16 1.521858606 0
17 2.221786357 0
18 1.702570220 0
19 2.546108904 0
20 3.293392415 0
21 2.508111439 0
22 2.141730799 0
23 1.575887826 0
24 2.924477661 0
25 -0.598490789 0
26 1.322472123 0
27 -0.220292151 0
28 -1.187760399 0
29 -0.704780429 0
30 0.049038865 0
31 1.107539957 0
32 0.508552938 0
33 0.682989533 0
34 2.316761839 0
35 2.054723505 0
36 1.525332222 0
37 0.535010531 0
38 2.212792969 0
39 3.096913065 0
40 -1.044458606 0
41 2.329454742 0
42 0.709540159 1
43 -0.696995161 1
44 -0.004208418 1
45 1.696906741 1
46 1.229724657 1
47 0.264262564 1
48 0.908514169 1
49 0.846353730 1
50 1.119740786 1
51 1.255598969 1
52 1.378676733 1
53 2.312124558 1
54 1.567975090 1
55 2.800926438 1
56 2.302996218 1
57 1.205560640 1
58 2.480827184 1
59 0.804859223 1
60 2.585180936 1
61 2.052002302 1
62 -1.605256848 1
63 -0.324148252 1
64 2.042973093 1
65 -1.197843626 1
66 1.363241155 1
67 1.123379478 1
68 2.093884474 1
69 -0.556921693 1
70 0.723599183 1
71 3.677745386 1
72 3.201192937 1
73 2.909777422 1
74 2.970150671 1
75 3.037157580 1
76 3.480819693 1
77 3.777144016 1
78 2.786905660 1
79 3.454894124 1
80 1.998585854 1
81 0.596828052 2
82 -0.022931314 2
83 -2.752492722 2
84 -0.472392961 2
85 0.266975232 2
86 -1.706853224 2
87 -2.621588416 2
88 -0.116622248 2
89 0.540872896 2
90 -0.940851574 2
91 -1.262513002 2
92 -0.608224164 2
93 1.819179692 2
94 0.620604861 2
95 0.380285117 2
96 -1.427896371 2
97 -1.364317065 2
98 3.454894124 2
99 1.998585854 2
100 2.329454742 2
101 -0.453356697 2
102 2.743415611 2
103 0.606260752 2
104 -0.405173898 2
105 2.723037509 2
106 0.659139394 2
107 2.463986995 2
108 -1.079504720 2
109 2.491884688 2
110 0.912952444 2
111 0.600674949 2
112 1.922284962 2
113 -1.134408563 2
114 2.078540579 2
115 0.054116644 2
116 -1.302556394 2
117 1.859866030 2
118 1.503511425 2
119 1.170173235 2
120 -0.805018678 2

#Fitting ANOVA
> model=aov(weightloss~pilltype,data=df)
> summary(model)
Df Sum Sq Mean Sq F value Pr(>F)
pilltype 2 28.58 14.288 6.439 0.00222 **
Residuals 117 259.62 2.219

---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Since p-value < 0.05, we reject the null hypothesis of no difference in weight loss and conclude that there is a significant difference in weight loss for different pills (experimental conditions).

> TukeyHSD(model)
Tukey multiple comparisons of means
95% family-wise confidence level

Fit: aov(formula = weightloss ~ pilltype, data = df)

$pilltype
diff lwr upr p adj
1-0 0.6665682 -0.1244112 1.4575476 0.1165845
2-0 -0.5344586 -1.3203545 0.2514373 0.2436170
2-1 -1.2010268 -1.9968116 -0.4052419 0.0014323

Since p-value for pill pair type (1,2) is < 0.05, we can say there is significant difference in weight loss for the two pill types.