-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_code.R
More file actions
53 lines (36 loc) · 1.22 KB
/
example_code.R
File metadata and controls
53 lines (36 loc) · 1.22 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
set.seed(4512)
rm(list=ls())
# Set your working directory to the folder containing presto_func.R
source("presto_func.R")
# Now the functions presto, which fits the model, and predictPrestoProbs, which
# generates class probabilties for unlabeled data, are loaded.
# Run the below line of code if you haven't already installed the ordinal
# package
# install.packages(“ordinal”)
library(ordinal)
# Load the soup data set
data(soup)
# Take a look at the data
print("Data set:")
str(soup)
print("Response (soup$SURENESS):")
y <- soup$SURENESS
print(str(y))
# Get the proportion of observations that lie in each class
print("Response class proportions:")
n <- length(y)
print(summary(y)/n)
dat <- model.matrix( ~ PROD + DAY + SOUPTYPE + SOUPFREQ + COLD + EASY +
GENDER + AGEGROUP + LOCATION, soup)
X <- dat[, colnames(dat) != "(Intercept)"]
stopifnot(nrow(X) == n)
stopifnot(ncol(X) == 22)
# Fit model on rest of data using selected lambda and get parameter estimates
# (takes about 3.5 minutes on my laptop)
print("Estimating PRESTO...")
presto_results <- presto(X, y, printIter=TRUE)
print("Estimated coefficients:")
print(presto_results)
print("Estimated probabilities:")
probs <- predictPrestoProbs(presto_results, X)
print(probs)