-
Notifications
You must be signed in to change notification settings - Fork 0
/
final.output.R
53 lines (39 loc) · 1.9 KB
/
final.output.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
final.output <- fucntion(resp, data, fit, prmt, contri) {
# prmt is a dataframe including 8 columns:
# variable, trans.meth, co.r, sc.1, sc.2, pc.r, oth, status
# Output:
# Needed Packages: "MASS" (for stepAIC()), "car" (for vif())
# 1.Transformation (trans.meth, co.r, sc.1, sc.2, pc.r, oth)
write.csv(prmt, paste(getwd(), "/prmt.history.csv", sep = ""))
message('Variable parameters history is exported to "prmt.csv" under default working directory')
cat(paste(rep("-+-",20),collapse=""))
# 2. Residuals
resid <- cbind(data[, resp], fit$fitted.values, summary(fit)$residuals)
rownames(resid) <- NULL
colnames(resid) <- c(resp, "Prediction", "Residuals")
write.csv(resid, paste(getwd(), "/residuals.csv", sep = ""))
message('Value of Response Variable, Prediction and Residuals
is exported to "residuals.csv" under default working directory')
cat(paste(rep("-+-",20),collapse=""))
# 3. Coefficients
coef <- coef(summary(fit)) # Estimate Std. Error t value Pr(>|t|)
# 4. VIF
if(length(coef(fit)) > 1) {
vif <- vif(fit)
}
# Merge model information into one data frame
# Output csv
prmt.alive <- prmt[which(prmt$status == "alive"), ][, -8]
if(rownames(coef)[1] == "(Intercept)"){
prmt.alive <- rbind(NA, prmt.alive)
vif <- rbind(NA, as.matrix(vif))
}
model <- as.data.frame(cbind(prmt.alive, coef, contri, vif))
rownames(model) <- NULL
colnames(model)[, -(1:11)] <- c("contri.rate", "VIF")
write.csv(model, paste(getwd(), "/model.results.csv", sep = "")))
message('Information of the Model
is exported to "model.results.csv" under default working directory')
cat(paste(rep("-+-",20),collapse=""))
# print(summary(stepAIC(fit, direction = "both", trace = 0)))
}