-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.Rmd
202 lines (126 loc) · 4.21 KB
/
index.Rmd
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
---
title: "Estimating and plotting generalized Euclidean preference parameters from conjoint data"
output:
html_document:
toc: true
number_sections: true
date: '2022-12-13'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(fixest)
library(tidyverse)
library(broom)
```
```{r, message = FALSE}
library(cjEuclid)
```
# Functions
The package has three functions and a wrapper.
## `lm_euclid`
`lm_euclid` fits a model with interactions and quadratic terms given a formula. It seeks to return the parameters of a generalized Euclidean model of the form:
For instance:
```{r}
r <- matrix(c(1, .15, .15, 2), 2, 2)
p <- c(.1, .5)
u <- function(butter = 0, guns = 0)
-(c(butter, guns) - p) %*% r %*% (c(butter, guns) -p)
data <-
fabricatr::fabricate(N = 100,
butter = rnorm(N),
guns = rnorm(N))
data$rating <- sapply(1:nrow(data), function(i) u(data$butter[i], data$guns[i]))
euclid <- lm_euclid(rating ~ guns + butter, data)
euclid
```
The function simply creates the Euclidian formula and passes it to `fixest`.
You can provide any additional `fixest` functions you want as additional arguments.
## `euclid_fits`
`euclid_fits` is a simple helper to generate fitted values for a model given specified ranges and length
```{r}
predictions_df <-
euclid_fits(formula = rating ~ guns + butter, euclid$model, data, lengths = c(2,3))
predictions_df |> kable(digits = 3)
```
## `euclid_plot`
`euclid_plot` call ggplot to plot the predicted fits and adds ideal points
```{r}
predictions_df <-
euclid_fits(formula = rating ~ guns + butter, euclid$model, data, lengths = c(20,20))
euclid_plot(predictions_df, X = "guns", Y = "butter")
```
## `cj_euclid`
`cj_euclid` wraps these functions and returns a list with the model, the fitted values, and the graph.
```{r}
euclid <- cj_euclid(rating ~ guns + butter, data)
euclid$graph
```
# Examples
The functions include multiple arguments to allow graphing multiple dimensions. Here we illustrate plotting 2 dimensions with faceting by a third.
```{r, fig.height = 4, fig.width = 14}
# Data has three dimensions and two observations per subject
library(fabricatr)
data <-
fabricate(
ID = add_level(20),
choice = add_level(2,
A = rnorm(N), B = rnorm(N), C = rnorm(N),
W = -(A^2 + (B-.3)^2 + (C --.67)^2) + .2*rnorm(N) + .5*A*B))
results <-
cj_euclid(W ~ A + B + C,
fixed_effects = "ID",
data = data,
X = "B",
Y = "C",
Col = "A",
mins = c(-2, -2, -2),
maxs = c(2, 2, 2),
lengths = c(5, 25, 35),
y_vals = c("little", "some", "much"),
x_vals = c("This", "that", "there"))
results
results$graph
```
more `ggplot`ting can be done later:
```{r, fig.height = 3, fig.width = 12}
results$graph +
ylab("another label and a flip") + coord_flip()
```
## 4 d
```{r, eval = TRUE}
# Data has three dimensions and two observations per subject
data <-
fabricate(
ID = add_level(20),
choice = add_level(2,
X1 = rnorm(N), X2 = rnorm(N), X3 = rnorm(N), X4 = rnorm(N),
W = -(X1^2 + (X2-.3)^2 + (X3 --.67)^2 + X4^2) + .2*rnorm(N)))
results <-
cj_euclid(W ~ X1 + X2 + X3 + X4,
fixed_effects = "ID",
data = data,
X = "X1",
Y = "X2",
Row = "X3",
Col = "X4",
mins = c(-2, -1, -2, -2),
maxs = c(2, 1, 2, 2),
lengths = c(35, 25, 5, 3),
y_vals = c("little", "some", "much"),
x_vals = c("This", "that", "there"))
results$graph
```
## Subgroup plots
Can be done by treating a subgroup as a variable, by stitching multiple plots together, or by running lm_euclid adn cj_Euclid on multiple groups and then applying the plot function with the group as a dimension.
## Example when $A$ is not positive semi definite
```{r}
data <-
fabricatr::fabricate(N = 40,
butter = rnorm(N),
guns = rnorm(N),
rating = -(butter^2 - (guns-.3)^2 + .2*rnorm(N)))
euclid <- cj_euclid(rating ~ guns + butter, data)
euclid
euclid$graph
```