-
Notifications
You must be signed in to change notification settings - Fork 0
/
Repeatr_4.R
61 lines (42 loc) · 1.48 KB
/
Repeatr_4.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
#' @name Repeatr_4
#' @title prepares data for choice modelling with mlogit, and estimates a basic choice model.
#' @description Defines indices, makes changes to variable formats and data structure to prepare for choice modelling with mlogit.
#'
#' @import dplyr
#' @import stringr
#' @import lubridate
#' @import fastDummies
#' @import rlang
#' @import knitr
#' @import mlogit
#'
#'
#' @param mydf optional dataframe to be used. If omitted, the default dataframe will be used. Example of use: ml_Repeatr4 <- Repeatr_4()
#'
#' @return
#' @export
#'
#' @examples
#'
Repeatr_4 <- function(mydf = NULL) {
mydir <- getwd()
myinputdir <- paste0(mydir, "/inst/extdata/")
mydatadir <- paste0(mydir, "/data")
# Choice modelling --------------------------------
if (is.null(mydf)==FALSE) {
Repeatr3 <- mydf
}
Repeatr4 <- Repeatr3
Repeatr4$case <- factor(as.numeric(as.factor(Repeatr4$case)))
Repeatr4$alt <- as.factor(Repeatr4$alt)
Repeatr4$choice <- as.logical(Repeatr4$choice)
Repeatr4 <- dfidx(Repeatr4, idx = c("case", "alt"), drop.index = FALSE)
ml.Repeatr4 <- mlogit(choice ~ yearsold_1 + yearsold_2 + yearsold_3 + yearsold_4 + yearsold_5
+ yearsold_6 + yearsold_7 + yearsold_8 , data = Repeatr4)
summary.ml.Repeatr4 <- summary(ml.Repeatr4)
results_ml_Repeatr4 <- as.data.frame(summary.ml.Repeatr4[["CoefTable"]])
setwd(mydatadir)
save(results_ml_Repeatr4, file = "results_ml_Repeatr4.rda")
setwd(mydir)
return(results_ml_Repeatr4)
}