-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.R
More file actions
138 lines (128 loc) · 5.61 KB
/
code.R
File metadata and controls
138 lines (128 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#Prashant Kumar
#____________________________________________________________________________________
library(readxl) #Using the library
Data_cholestrol <- read_excel("dataset.xlsx")
N <- length(Data_cholestrol$Subject) #total number of data
LDL <- c(Data_cholestrol$PE,Data_cholestrol$AE,Data_cholestrol$BE,Data_cholestrol$PD,Data_cholestrol$AD,Data_cholestrol$BD)#Assigning all the group data to vector LDL
Factor_lifestyle <- c(rep("Exercise",3*N), rep("Diet",3*N))
Drugs <- c(rep("Placebo",N),rep("DrugA",N),rep("DrugB",N),rep("Placebo",N),rep("DrugA",N),rep("DrugB",N))#Group
Data <- data.frame(Drugs,Factor_lifestyle,LDL) #Using data.frame to adjust and form Nx3 data set
colnames(Data) <- c("Drugs","Factors(lifestyle)","LDL[mg/L]") #Renaming the columns
print(Data)
#________________________________________________________________________________________
#TWO WAY ANOVA
#Anova test
Anova <- aov(LDL~Drugs + Factor_lifestyle,data = Data)
print(summary(Anova))
#________________________________________________________________________________________
#Visualization
library(ggpubr)
library(ggplot2)
#Box plot
boxplt <-ggboxplot(Data, x = "Drugs", y = "LDL[mg/L]", color = "Factors(lifestyle)",
palette = c("#00AFBB", "#E7B800"))
boxplt
#Interaction plot
plot_inter <- ggline(Data, x = "Drugs", y = "LDL[mg/L]", color = "Factors(lifestyle)",
add = c("mean_se", "dotplot"),
palette = c("#00AFBB", "#E7B800"))
plot_inter
#______________________________________________________________________________________
#post hoc test
#conducting pairwise t test
test_pairwise <- pairwise.t.test(Data$`LDL[mg/L]`, Data$Drugs,
p.adjust.method = "BH")
test_pairwise
#__________________________________________________________________________________________
#____________________________________________________________________________________________
#____________________________________________________________________________________________
#Linear Regression
library(readxl)
Data_reg <- read_excel("dataset.xlsx",
sheet = "Linear regression")
#Mean
cat("Mean of Explanatory variable is",mean(Data_reg$x))
cat("Mean of Dependent variable is",mean(Data_reg$y))
#Variance
cat("Variance of Explanatory variable is",var(Data_reg$x))
cat("Variance of Dependent variable is",var(Data_reg$y))
#Scatterplot
library(ggplot2)
plt1 <- ggplot() +
geom_point(aes(x = Data_reg$x, y = Data_reg$y))+
theme_bw()+
labs( x = "Weight[mg]",y = "LDL[mg/L]")+
ggtitle("Scatter plot b/w LDL[mg/L] and Weight[mg]")+
theme( axis.text = element_text( size =12) ,
axis.title = element_text( size =12) ,
plot.title = element_text( hjust = 0.5))
plt1
#__________________________________________________________________________________________
#Using glm command to analyse the standard linear model
model <- glm(Data_reg$y ~ Data_reg$x, data = Data_reg)
summary(model)
#_________________________________________________________________________________________
#Calculating the slope m and intercept b
b = model$coefficients[1]
m = model$coefficients[2]
m
b
#For calculating the confidence interval of b and m we have to first compute standord errors
#Computation of Standard error of Intercept i.e. SEb
SEb <- summary(model)$coefficients[,2][1]
#Computation of standard error of slope(m) i.e. SEm
SEm <- summary(model)$coefficients[,2][2]
SEb
SEm
#Assigning predicted values as regression
regression <- (m*Data_reg$x) + b
#Computation of Confidence interval
#For slope
CIm_max <- m + qt(0.985,(length(Data_reg$`Subject ID`)-2))*SEm
CIm_min <- m - qt(0.985,(length(Data_reg$`Subject ID`)-2))*SEm
#For Intercept
CIb_max <- b + qt(0.985,(length(Data_reg$`Subject ID`)-2))*SEb
CIb_min <- b - qt(0.985,(length(Data_reg$`Subject ID`)-2))*SEb
#printing the confidence interval for slope and Intercept
CIm_max
CIm_min
CIb_max
CIb_min
#___________________________________________________________________________________________
library(ggplot2)
library(ggpubr)
#Regression maximum and minimum line
Reg_max <- (CIm_max * Data_reg$x) + CIb_min
Reg_min <- (CIm_min * Data_reg$x) + CIb_max
#plotting the scatterplot
plt2 <- ggplot() +
geom_point( aes(x = Data_reg$x , y= Data_reg$y ))+
geom_line( aes(x= Data_reg$x , y= regression ) , color = " blue ")+ # Optimal regression line
geom_line( aes(x= Data_reg$x , y= Reg_max ) , color = " red ")+ # regression with upper -limit slope
geom_line( aes(x= Data_reg$x, y= Reg_min ) , color = " red ")+ # Regression with lower -limit slope
geom_smooth( aes( x= Data_reg$x , y= Data_reg$y ) , method = lm)+ #Auto - generated slope
stat_regline_equation( aes(x= Data_reg$x, y= Data_reg$y) )+ # Inserting regression equation
theme_bw() +
labs (x =" Weight[mg]",
y = " LDL[mg/L]")+
ggtitle(" LDL[mg/L] VS Weight[mg]")+
theme( axis.text = element_text ( size =12) ,
axis.title = element_text ( size =12) ,
plot.title = element_text ( hjust = 0.5) )
plot(plt2)
#_______________________________________________________________________________________________
library(ggplot2)
e <-regression - Data_reg$y #computing residual
#Scatterplot of the residual
library(ggplot2)
plt3 <- ggplot() +
geom_point(aes(x = Data_reg$y, y = e))+
geom_line(aes(x = Data_reg$y, y = 0), color = "Red")+
theme_bw()+
labs( x = "LDL[mg/L]",y = "Residual")+
ggtitle("Scatter plot b/w LDL[mg/L] and Residual")+
theme( axis.text = element_text( size =12) ,
axis.title = element_text( size =12) ,
plot.title = element_text( hjust = 0.5))
plt3
#_______________________________________________________________________________________________________