-
Notifications
You must be signed in to change notification settings - Fork 0
/
sem_test.R
127 lines (99 loc) · 2.5 KB
/
sem_test.R
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
# clear all
rm(list=ls())
library('dplyr')
library(lavaan)
require(compiler)
validity_accuracy = rep(0, times = 1000)
train_accuracy = rep(0,times = 1000)
# 1000 trials
for (trials in 1:1000){
# Load files
data=read.csv("test.csv",header=TRUE)
# remove na
data <- data[!(rowSums(is.na(data))),]
# get 1/3 of the dataset for validation set
length = nrow(data)
valid_length = round(length/3)
valid_data = sample_n(data,valid_length)
# extract train data
datalist = data.frame()
nowcount = 1
for (i in 1:length){
flag = 0
for (j in 1:valid_length){
# somewhere in the validset?
if (data$ID[i] == valid_data$ID[j]){
# set flag and end
flag = 1
break
}
}
# not in valid dataset? then add
if (flag == 0){
data_frame <- data[i,]
datalist <- rbind(datalist , data_frame)
nowcount=nowcount + 1
}
}
train_data <- datalist
# sem
# modification
mod_model1 <- '
# Touch
Touch ~ Int
# Excel
Excel ~ Int
# Diff
Diff ~ attn + Vols
# Final output
pVolsOX ~ attn + Vols + Excel
Touch ~~ Excel
'
modified1 <- sem(mod_model1, data = train_data, ordered = 'pVolsOX')
# predict training set
# train_prob <- sem.predict(modified1, train_data, type="response")
coefs <- parTable(modified1)
results <- (train_data$Int * coefs[2,14] + coefs[25,14]) * coefs[7,14] + coefs[5,14] * train_data$att + coefs[6,14] * train_data$Vols
train_acc = 0
train_length = nowcount-1
num_train = 0
for (i in 1:train_length){
# null value?
if (!is.na(results[i])){
pox = 0
if (as.numeric(results [i]) >= coefs[9,14]){
pox = 1
}
if (pox == train_data$pVolsOX[i]){
train_acc = train_acc + 1
}
num_train = num_train + 1
}
}
# predict validation dataset
# valid_prob <- sem.predict(modified1, valid_data, type="response")
results1 <- (valid_data$Int * coefs[2,14] + coefs[25,14]) * coefs[7,14] + coefs[5,14] * valid_data$att + coefs[6,14] * valid_data$Vols
valid_acc = 0
num_valid = 0
for (i in 1:valid_length){
# null value?
if (!is.na(results1[i])){
pox = 0
if (as.numeric(results1 [i]) >= coefs[9,14]){
pox = 1
}
if (pox == valid_data$pVolsOX[i]){
valid_acc = valid_acc + 1
}
num_valid = num_valid + 1
}
}
# store results
validity_accuracy [trials] = valid_acc / num_valid
train_accuracy [trials] = train_acc / num_train
# current done
#print(trials)
}
# save results
result <- data.frame(train_accuracy,validity_accuracy)
write.csv(result,file="sem_result.csv")