Repository navigation
Expand file tree
/
Copy pathstats.R
More file actions
175 lines (116 loc) · 4.89 KB
/
Copy pathstats.R
File metadata and controls
175 lines (116 loc) · 4.89 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#library(tidyverse)
#library(dplyr)
#library(ggplot2)
#library(summarytools)
# create df
# normal distribution with 2 groups
df <- data.frame(nums=rnorm(90),type=rep(c("a","b"), times=45))
df$row <- row(df)[,1]
df$nums_type <- as.integer(factor(df$type))
################################################################################
# independent samples t-test
t_tst <- function(xx,yy){
val <- ((mean(xx) - mean(yy))/sqrt((sd(xx)^2/length(xx))+(sd(yy)^2/length(yy))))
return (val)
}
#t-test
df_x <- df[df$type=="a",]
df_x <- df_x$nums
df_y <- df[df$type=="b",]
df_y <- df_y$nums
t_tst <- t_tst(df_x,df_y) # custom function
t.test(nums~type, data=df) # or use built-in function
# sig and p-value mean what
# p <= 0.05 = accept h1 reject h0
# h0 is there is no difference between the types
# h1 is there is a significant difference between the types
### correlation test
cor1 <- cor.test(formula = ~ nums+nums_type, data=df)
cor1 <- cor.test(x=df$nums, y=df$nums_type)
# sig and p-value mean what
# p <= 0.05 = accept h1 reject h0
# h0 is there is no difference between the types
# h1 is there is a significant difference between the types
################################################################################
##### chi-square test
# one-sample, based on nums_type (2 possibilities)
chi1 <- chisq.test(x=table(df$type)) # category
chi1 <- chisq.test(x=table(df$nums_type)) # or number as category
# sig and p-value mean what
# p <= 0.05 = accept h1 reject h0
# h0 is there is no difference between the types
# h1 is there is a significant difference between the types
# 2-sample
chi2 <- chisq.test(x=table(df$type,df$nums_type))
# sig and p-value mean what
################################################################################
##### anova
# one-way anova
# https://bookdown.org/ndphillips/YaRrr/ex-one-way-anova.html
aov1 <- aov(formula = nums ~ factor(type), data=df)
# view summary
aov_sum <- summary(aov1)
# post-hoc tests, Tukey HSD
aov_tky <- TukeyHSD(aov1)
# p <= 0.05 = accept h1 reject h0
# h0 is there is no difference between the types
# h1 is there is a significant difference between the types
# lm - same formula as anova to look at coefficients
lm1 <- lm(formula = nums ~ factor(type), data=df)
lm1_sum <- summary(lm1)
# two-way anova
################################################################################
# mann-whitney u test
# test whether there is a difference in the dependent variable in two independent groups
# in this case testing the type "a" or "b" against the nums (values) within each group
mwu <- wilcox.test(df$nums~factor(df$type))
# print the median and interquartile ranges
med_df <- data.frame(tapply(df$nums,df$type,median,na.rm=T))
iqr_df <- data.frame(tapply(df$nums,df$type,IQR,na.rm=T))
print(paste("mwu",med_df, iqr_df))
# if p_value >= 0.05 no significant difference
# h0 is there is no difference between the types
# h1 is there is a significant difference between the types
################################################################################
##### linear model
# https://bookdown.org/ndphillips/YaRrr/the-linear-model.html
# make linear model
lm1 <- lm(nums~row, data=df)
# view summary of model
lm1_sum <- summary(lm1)
# see details of coefficients
lm1_sum_co <- summary(lm1)$coefficients
# add fitted values back to df
df$lm_fits <- lm1$fitted.values
# plot the predicted values against the true values
plot(x = df$nums, # True values on x-axis
y = df$lm_fits, # fitted values on y-axis
xlab = "True Values",
ylab = "Model Fitted Values",
main = "Regression fits of diamond values")
abline(b = 1, a = 0) # Values should fall around this line!
# predict on new values
new_df <- data.frame(row=91:100)
new_preds <- predict(object=lm1, newdata=new_df)
new_df$preds <- new_preds
######## linear model with interactions
# if using interactions in model need to use mean adjusted values
# created centered versions
df$centered_row <- df$row - mean(df$row)
# create centered lm
lm1_c <- lm(nums~centered_row, data=df)
# summary of centered lm
summary(lm1_c)$coefficients
# ANOVA object from regression
lm_aov <- summary(aov(lm1))
lm_c_aov <- summary(aov(lm1_c))
####### regression on non normal data
# use generalised linear model glm
# binary flag
df$flag <- df$nums > 0.05 #ifelse(df$nums > 0.05,1,0)
# glm based on true (1) or false (0)
glm1 <- glm(formula = flag~row, data = df, family = "binomial") # family flag can be adjusted as needed
# access fitted values the same way
df$glm_fits <- glm1$fitted.values
# as logit transformations, to get percentage predictions need to inverse logit
new_df$glm_preds <- 1/predict(object=glm1,newdata=new_df) # percentage that value is greater than 0.05