forked from nkpizano/IMMERSE_Day-5
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMultinomial Regression.Rmd
More file actions
263 lines (200 loc) · 7.4 KB
/
Copy pathMultinomial Regression.Rmd
File metadata and controls
263 lines (200 loc) · 7.4 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
---
title: "IMMERSE_Day 5_Multinomial Regression"
author: "Netasha"
date: "2024-05-20"
output: html_document
---
1. From github select github repository
2. Click the "<> Code"
3. Click Download Zip
4. Move Zip file to desktop
5. Make sure file is unzipped!
6. Open IMMERSE_Day-5.Rproj project
7. Select the "Multinomial regression.Rmd"
## Read in packages
There are a few packages we want our markdown to read in:
Click on the "run current chunk" button (looks like a play button on line 19 )
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(MplusAutomation) #use this code to run mplus code in R
library(tidyverse) #collection of R packages designed for data science
library(here) #helps with filepaths
library(haven) # read_sav()
library(psych) # describe()
library(ggpubr) # ggdensity() and ggqqplot()
library(corrplot) # corrplot()
here::i_am("Multinomial Regression.Rmd")
```
## Overview: Camera Marketing Study
Sample of 735 of individuals surveyed by a market research group for the purposes of investigating the role of age and “gender” (outdated binary—this is old data) in digital camera brand choices.
Variables for the study include:
1. Brand - 1 = Cannon - 2 = Kodak - 3 = Nikon
2. Female - 1 = Female - 0 = Male
3. Age (in years)
## Import Data
In R:
- Start a new code chunk to load the data
- PC: Ctrl + Alt + I
- Mac: Option + Command + I
```{r}
#Using the read_csv code we will import our .dat data file into R. We are going to add column names to descriptive purposes
#there are many ways to import your data such as the import dataset menu in the environment (point and click approach)
data_csv <- read_csv(here("camera.dat"), col_names = c("brand", "female", "age"))
```
## Evaluate Data
Create a new code chunk
Use the summary function to get summary of the data
Lets make a contingency (3x2) table to get a snapshot of camera choice using the table function
- Start a new code chunk to load the data
- PC: Ctrl + Alt + I
- Mac: Option + Command + I
- use the summary function
- use the table function
- create a name for the table
- Assign the table using assignment operator
- PC: Alt + (-)
- Mac: Option + (-)
- type table
- type the name of the data set
- type $
-use the drop down menu to locate the variable or begin to type the variable name "brand" (x-axis) and select
- add a comma to add the second variable
- type the name of the data set followed by the dollar sign and "female" (y-axis)
- note: the table function will give a basic table. Labels will not be presented on the x and y axis. We would need to change the variables to factors.
```{r}
summary(data_csv)
table1 <-table(data_csv$brand, data_csv$female)
table1
```
## Prepare data for mplus
Introduced last week:
Convert from file to Mplus *.dat* file**
Say you want an Mplus .dat file and don't want to go through the hassle of deleting rows and manual conversion to *.dat*.
You can use the `prepareMplusData()` function to convert from *.csv* to *.dat*. A new file will be generated in your folder
create a new code chunk
```{r}
prepareMplusData(data_csv, here("exp_data.dat"))
```
## Using `MplusAutomation` for multinomial regression
Reminder from last week:
**What does the** `mplusObject()` **function do?**
1. It generates an Mplus input file (does not need full variable name list, its automated for you!)
2. It generates a datafile specific to each model
3. It runs or estimates the model (hopefully) producing the correct output. Always check!
**What does the** `mplusModeler()` **function do?**
1. Creates, runs, and reads Mplus models created using `mplusObject()`
2. You can specify where you want the *.out* file saved
3. `check=TRUE` checks for missing semicolons, `run=TRUE` runs the model, `hashfilename=FALSE` does not add a hash of the raw data to the data file name.
create a new code chunk
### Mulinomial regression of brand on female simple
```{r, echo=TRUE, eval=FALSE}
m_basic_simp <- mplusObject(
TITLE = "Multinomial regression brand on female;",
VARIABLE =
"usevar = brand female;
Nominal are brand;",
ANALYSIS =
"estimator = ML;",
MODEL =
"brand on female;",
usevariables = colnames(data_csv),
rdata = data_csv)
m_basic_fit_simp <- mplusModeler(m_basic_simp,
dataout=here("basic_simp.dat"),
modelout=here("basic_simp.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```
### Multiple regression with MLR and selected output
```{r, echo=TRUE, eval=FALSE}
m_basic <- mplusObject(
TITLE = "Multinomial regression brand on female;",
VARIABLE =
"usevar = brand female;
Nominal are brand;",
ANALYSIS =
"estimator = MLR;",
MODEL =
"brand on female;",
OUTPUT =
"sampstat crosstabs residual cinterval tech1 svalues;",
usevariables = colnames(data_csv),
rdata = data_csv)
m_basic_fit <- mplusModeler(m_basic,
dataout=here("basic.dat"),
modelout=here("basic.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```
CHECK YOUR OUTPUT!!! Select .out to view output!
### Use final estimates as starting values in the command line
create new code chunk
-Mulinomial regression of brand on female with starting values from last output.
- must add a * before start value
-Add parameter labels to each level of brand in parenthesis
```{r, echo=TRUE, eval=FALSE}
m_basic_2 <- mplusObject(
TITLE = "Multinomial regression brand on female;",
VARIABLE =
"usevar = brand female;
Nominal are brand;",
ANALYSIS =
"estimator = MLR;",
MODEL =
"brand#1 on female* -0.38299 (femCvN);
brand#2 on female* -0.13628 (femKvN);
[brand#1*0.16508] (intCvN);
[brand#2*0.23841] (intKvN);",
MODELTEST =
"0 = femCvN;
0 = femKvN;",
usevariables = colnames(data_csv),
rdata = data_csv)
m_basic_fit_2 <- mplusModeler(m_basic_2,
dataout=here("basic_2.dat"),
modelout=here("basic_2.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```
### Mplus input with alternate COR/RR
```{r, echo=TRUE, eval=FALSE}
m_basic_3 <- mplusObject(
TITLE = "Multinomial regression brand on female;",
VARIABLE =
"usevar = brand female;
Nominal are brand;",
ANALYSIS =
"estimator = MLR;",
MODEL =
"brand#1 on female* -0.38299 (femCvN);
brand#2 on female* -0.13628 (femKvN);
[brand#1*0.16508] (intCvN);
[brand#2*0.23841] (intKvN);",
MODELCONSTRAINT =
"New(femCvK efemCvK);
femCvK = femCvN-femKvN;
efemCvK = exp(femCvk);",
usevariables = colnames(data_csv),
rdata = data_csv)
m_basic_fit_3 <- mplusModeler(m_basic_3,
dataout=here("basic_3.dat"),
modelout=here("basic_3.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```
### Mplus input multinomial regression of brand on female & age
```{r, echo=TRUE, eval=FALSE}
m_basic_4 <- mplusObject(
TITLE = "Multinomial regression brand on female and age;",
VARIABLE =
"usevar = brand female age;
Nominal are brand;",
ANALYSIS =
"estimator = MLR;",
MODEL =
"brand on female age",
OUTPUT =
"sampstat crosstabs residual cinterval tech1 svalues;",
usevariables = colnames(data_csv),
rdata = data_csv)
m_basic_fit_4 <- mplusModeler(m_basic_4,
dataout=here("basic_4.dat"),
modelout=here("basic_4.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)
```