In: Computer Science
***This problem must be done using R so please provide the R code used to find the solution. I have provided the data in data-wtLoss.txt below the question. I will also give "thumbs-up for correct R code" Thanks in advance.***
The file “data-wtLoss.txt” contains data on weight loss and self esteem evaluation at three time points over a period of three months for 34 individuals who are randomly selected from a residential area. These individuals are randomly assigned to one of the three treatment groups: control, diet and diet plus exercise.
Construct bar plot and pie chart to display summary information of the treatment groups. Please be considerate to readers and make your graphs as informative and concise as possible by using legend, labeling axes, displaying numbers and so on. You may consider ordering the layout based on the relative frequency. ***
***Here is the rest of the data needed to solve the problem. Please solve using R. Thank you!***
ID group wl1
wl2 wl3 se1 se2
se3 bw
1 Control 3.3 2.9
2.5 13.9 13.3 15.5
127.5
2 Control 3.8 3.9
3.8 13.2 14.1 16.5
130.7
3 Control 4.2 3.4
-0.5 17 12 15.7
129.6
4 Control 3.4 0.9
0.6 11.2 11.1 11.5
134.4
5 Control 6.1 1.8
1.2 15.8 15 12.5
130.6
6 Control 7 6.6
3.6 15.9 17 18.2
131.6
7 Control 5.9 2.9
5.2 17.1 16.2 19.6
127.1
8 Control 6.4 4
0.9 13.3 14.8 15.3
133.6
9 Control 3.2 2.9
1.2 13.5 14.6 14.3
125.9
10 Control 3.6 3.3
2.3 13.4 16.1 12.6
128.2
11 Control 3.5 3
1.4 15.8 15.4 10.1
130.4
12 Control 6.3 -0.1
-0.4 14.7 13.3 16.6
130.5
13 Diet 5.6 3.9
1.7 12.2 11.1 13.8
129
14 Diet 6.3 4
1.1 13.1 13.7 14.5
133.7
15 Diet 7 4.7
4 17.5 11.3 18.4
130.6
16 Diet 4.1 2.1
1.7 16.8 15.4 17.9
129.9
17 Diet 2.6 2.7
0.7 16 17 14.2
128.1
18 Diet 3.3 4.8
5.6 13 10.9 18
132.6
19 Diet 4.2 3.2
0.2 12.1 10.5 14
125.4
20 Diet 5.9 2.8
1.4 11.8 10.7 11.1
141.6
21 Diet 3.7 6.7
2.4 16.9 15.9 19.2
127.8
22 Diet 8 5.9
4.4 19 18.9 18.7
133.8
23 Diet 2.6 2.4
0.5 15.1 15.5 15.3
131.3
24 Diet 8.8 5.4
2.5 16.1 13.9 17.4
133.9
25 DietEx 8.4 6.1
3.7 14.9 11.9 19.7
125.9
26 DietEx 3.2 5.8
2 16 11.8 17.7
127.8
27 DietEx 8.7 6.5
2.5 12.9 12.8 17.2
126.4
28 DietEx 2.9 6.4
1.3 16.4 13.1 17.3
131.2
29 DietEx 2.6 4.2
0.6 13 12.5 15.9
124.2
30 DietEx 8.6 5.4
1.1 14.7 12.3 17.2
131.2
31 DietEx 6.1 4.8
3.7 14.9 13 17.8
129.5
32 DietEx 8.3 3.8
1.5 15.7 13.8 16.7
138.8
33 DietEx 7.6 8.7
5.3 16.1 16.4 18.8
129.3
34 DietEx 8.2 7.4
1.4 17.4 17.3 17.4
129.4
The desired R -Code is given below:
#I have saved the data in excel file then import it
#You can do it directly by entering the data, name the data frame
as df, then all commands will work
library(readxl)
df<- read_excel("C:/Users/91773/Desktop/data.xlsx")
df
wl_sum<-df$wl1+df$wl2+df$wl3
df<-cbind(df,wl_sum)
# total wl column added to last of the data frame
df
#Sorted order
# negative sign used to order in decreasing order
df<-df[order(-df$wl_sum),]
df
#Box-plot using ggplot2 package---install if you don't have
graphics.off()
library(ggplot2)
ggplot(df,aes(group,wl_sum),fill=wl_sum)+geom_boxplot()+stat_summary(fun="mean",color="red",shape=15)
R-Output
> #I have saved the data in excel file then import it
> #You can do it directly by entering the data, name the data
frame as df, then all commands will work
> library(readxl)
> df<- read_excel("C:/Users/91773/Desktop/data.xlsx")
> df
# A tibble: 34 x 9
ID group wl1 wl2 wl3 se1 se2 se3 bw
<dbl> <chr> <dbl> <dbl> <dbl>
<dbl> <dbl> <dbl> <dbl>
1 1 Control 3.3 2.9 2.5 13.9 13.3 15.5 128.
2 2 Control 3.8 3.9 3.8 13.2 14.1 16.5 131.
3 3 Control 4.2 3.4 -0.5 17 12 15.7 130.
4 4 Control 3.4 0.9 0.6 11.2 11.1 11.5 134.
5 5 Control 6.1 1.8 1.2 15.8 15 12.5 131.
6 6 Control 7 6.6 3.6 15.9 17 18.2 132.
7 7 Control 5.9 2.9 5.2 17.1 16.2 19.6 127.
8 8 Control 6.4 4 0.9 13.3 14.8 15.3 134.
9 9 Control 3.2 2.9 1.2 13.5 14.6 14.3 126.
10 10 Control 3.6 3.3 2.3 13.4 16.1 12.6 128.
# ... with 24 more rows
> wl_sum<-df$wl1+df$wl2+df$wl3
> df<-cbind(df,wl_sum)
> # total wl colun added to last of the data frame
> df
ID group wl1 wl2 wl3 se1 se2 se3 bw wl_sum
1 1 Control 3.3 2.9 2.5 13.9 13.3 15.5 127.5 8.7
2 2 Control 3.8 3.9 3.8 13.2 14.1 16.5 130.7 11.5
3 3 Control 4.2 3.4 -0.5 17.0 12.0 15.7 129.6 7.1
4 4 Control 3.4 0.9 0.6 11.2 11.1 11.5 134.4 4.9
5 5 Control 6.1 1.8 1.2 15.8 15.0 12.5 130.6 9.1
6 6 Control 7.0 6.6 3.6 15.9 17.0 18.2 131.6 17.2
7 7 Control 5.9 2.9 5.2 17.1 16.2 19.6 127.1 14.0
8 8 Control 6.4 4.0 0.9 13.3 14.8 15.3 133.6 11.3
9 9 Control 3.2 2.9 1.2 13.5 14.6 14.3 125.9 7.3
10 10 Control 3.6 3.3 2.3 13.4 16.1 12.6 128.2 9.2
11 11 Control 3.5 3.0 1.4 15.8 15.4 10.1 130.4 7.9
12 12 Control 6.3 -0.1 -0.4 14.7 13.3 16.6 130.5 5.8
13 13 Diet 5.6 3.9 1.7 12.2 11.1 13.8 129.0 11.2
14 14 Diet 6.3 4.0 1.1 13.1 13.7 14.5 133.7 11.4
15 15 Diet 7.0 4.7 4.0 17.5 11.3 18.4 130.6 15.7
16 16 Diet 4.1 2.1 1.7 16.8 15.4 17.9 129.9 7.9
17 17 Diet 2.6 2.7 0.7 16.0 17.0 14.2 128.1 6.0
18 18 Diet 3.3 4.8 5.6 13.0 10.9 18.0 132.6 13.7
19 19 Diet 4.2 3.2 0.2 12.1 10.5 14.0 125.4 7.6
20 20 Diet 5.9 2.8 1.4 11.8 10.7 11.1 141.6 10.1
21 21 Diet 3.7 6.7 2.4 16.9 15.9 19.2 127.8 12.8
22 22 Diet 8.0 5.9 4.4 19.0 18.9 18.7 133.8 18.3
23 23 Diet 2.6 2.4 0.5 15.1 15.5 15.3 131.3 5.5
24 24 Diet 8.8 5.4 2.5 16.1 13.9 17.4 133.9 16.7
25 25 DietEx 8.4 6.1 3.7 14.9 11.9 19.7 125.9 18.2
26 26 DietEx 3.2 5.8 2.0 16.0 11.8 17.7 127.8 11.0
27 27 DietEx 8.7 6.5 2.5 12.9 12.8 17.2 126.4 17.7
28 28 DietEx 2.9 6.4 1.3 16.4 13.1 17.3 131.2 10.6
29 29 DietEx 2.6 4.2 0.6 13.0 12.5 15.9 124.2 7.4
30 30 DietEx 8.6 5.4 1.1 14.7 12.3 17.2 131.2 15.1
31 31 DietEx 6.1 4.8 3.7 14.9 13.0 17.8 129.5 14.6
32 32 DietEx 8.3 3.8 1.5 15.7 13.8 16.7 138.8 13.6
33 33 DietEx 7.6 8.7 5.3 16.1 16.4 18.8 129.3 21.6
34 34 DietEx 8.2 7.4 1.4 17.4 17.3 17.4 129.4 17.0
> #Sorted order
> # negative sign used to order in decreasing order
> df<-df[order(-df$wl_sum),]
> df
ID group wl1 wl2 wl3 se1 se2 se3 bw wl_sum
33 33 DietEx 7.6 8.7 5.3 16.1 16.4 18.8 129.3 21.6
22 22 Diet 8.0 5.9 4.4 19.0 18.9 18.7 133.8 18.3
25 25 DietEx 8.4 6.1 3.7 14.9 11.9 19.7 125.9 18.2
27 27 DietEx 8.7 6.5 2.5 12.9 12.8 17.2 126.4 17.7
6 6 Control 7.0 6.6 3.6 15.9 17.0 18.2 131.6 17.2
34 34 DietEx 8.2 7.4 1.4 17.4 17.3 17.4 129.4 17.0
24 24 Diet 8.8 5.4 2.5 16.1 13.9 17.4 133.9 16.7
15 15 Diet 7.0 4.7 4.0 17.5 11.3 18.4 130.6 15.7
30 30 DietEx 8.6 5.4 1.1 14.7 12.3 17.2 131.2 15.1
31 31 DietEx 6.1 4.8 3.7 14.9 13.0 17.8 129.5 14.6
7 7 Control 5.9 2.9 5.2 17.1 16.2 19.6 127.1 14.0
18 18 Diet 3.3 4.8 5.6 13.0 10.9 18.0 132.6 13.7
32 32 DietEx 8.3 3.8 1.5 15.7 13.8 16.7 138.8 13.6
21 21 Diet 3.7 6.7 2.4 16.9 15.9 19.2 127.8 12.8
2 2 Control 3.8 3.9 3.8 13.2 14.1 16.5 130.7 11.5
14 14 Diet 6.3 4.0 1.1 13.1 13.7 14.5 133.7 11.4
8 8 Control 6.4 4.0 0.9 13.3 14.8 15.3 133.6 11.3
13 13 Diet 5.6 3.9 1.7 12.2 11.1 13.8 129.0 11.2
26 26 DietEx 3.2 5.8 2.0 16.0 11.8 17.7 127.8 11.0
28 28 DietEx 2.9 6.4 1.3 16.4 13.1 17.3 131.2 10.6
20 20 Diet 5.9 2.8 1.4 11.8 10.7 11.1 141.6 10.1
10 10 Control 3.6 3.3 2.3 13.4 16.1 12.6 128.2 9.2
5 5 Control 6.1 1.8 1.2 15.8 15.0 12.5 130.6 9.1
1 1 Control 3.3 2.9 2.5 13.9 13.3 15.5 127.5 8.7
11 11 Control 3.5 3.0 1.4 15.8 15.4 10.1 130.4 7.9
16 16 Diet 4.1 2.1 1.7 16.8 15.4 17.9 129.9 7.9
19 19 Diet 4.2 3.2 0.2 12.1 10.5 14.0 125.4 7.6
29 29 DietEx 2.6 4.2 0.6 13.0 12.5 15.9 124.2 7.4
9 9 Control 3.2 2.9 1.2 13.5 14.6 14.3 125.9 7.3
3 3 Control 4.2 3.4 -0.5 17.0 12.0 15.7 129.6 7.1
17 17 Diet 2.6 2.7 0.7 16.0 17.0 14.2 128.1 6.0
12 12 Control 6.3 -0.1 -0.4 14.7 13.3 16.6 130.5 5.8
23 23 Diet 2.6 2.4 0.5 15.1 15.5 15.3 131.3 5.5
4 4 Control 3.4 0.9 0.6 11.2 11.1 11.5 134.4 4.9
> #Box-plot using ggplot2 package---install if you don't
have
> graphics.off()
> library(ggplot2)
>
ggplot(df,aes(group,wl_sum),fill=wl_sum)+geom_boxplot()+stat_summary(fun="mean",color="red",shape=15)
Obtained Box-Plot
Interpretation:
From the box plot it is clear that Diet with exercise shown the best result of weight loss while control shows the least effect in comparison diet and DietEx.