-
Notifications
You must be signed in to change notification settings - Fork 0
/
STAT462_Lab3.Rmd
404 lines (255 loc) · 18.8 KB
/
STAT462_Lab3.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
---
title: "Lab 3: Scatterplots"
subtitle: <h5 style="font-style:normal">STAT-462 - Regression Analysis</h4>
author: <h5 style="font-style:normal">Dr Helen Greatrex</h4>
output:
html_document:
toc: true
toc_depth: 4
toc_float:
collapsed: no
number_sections: no
---
<style>
p.comment {
background-color: #DBDBDB;
padding: 10px;
border: 1px solid black;
margin-left: 0px;
border-radius: 5px;
font-style: normal;
}
h1.title {
font-weight: bold;
font-family: Arial;
}
h2.title {
font-family: Arial;
}
</style>
<style type="text/css">
#TOC {
font-size: 12px;
font-family: Arial;
}
</style>
\
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message = FALSE)
library(tidyverse)
library(dplyr)
library(ggpubr)
library(skimr)
library(ggplot2)
library(plotly)
library(ISLR)
library(equatiomatic)
library(olsrr)
library(Stat2Data)
library(readxl)
library(corrplot)
data("HousesNY")
```
# Learning objectives
By the end of this week's lab, you will be able to:
1. Understand YAML code and set up a lab template
2. Get comfortable de-bugging errors
2. Conduct some exploratory data analysis
3. Make better plots and a more professional report
<br>
<p class="comment">**Assignment 2 is due by midnight Next Wed.** [See here]https://psu.instructure.com/courses/2174925/assignments/13762818) I PROVIDE HELP UNTIL THE END OF NEXT WEEK'S LAB (final evening is for your own finishing up).</p>
<br>
## I need help
1. There is a TEAMS discussion for lab help [CLICK HERE](https://teams.microsoft.com/l/team/19%3aWabo92vghie-p1jKkmYOGJIOPMUExkoPb0JQMb_9dgw1%40thread.tacv2/conversations?groupId=bbc92dcc-56df-48e6-8da3-5cd766908eeb&tenantId=7cf48d45-3ddb-4389-a9c1-c115526eb52e). Remember to include a screenshot of the issue and a short description of the problem. Also try googling the error first.
2. Every time you re-open R studio check you are using your project file (does it say Lab 3 at the top?).
3. EVERY TIME YOU RE-OPEN R-STUDIO YOU NEED TO RE-RUN **ALL** YOUR CODE CHUNKS. The easiest way to do this is to press the "Run All" button (see the Run menu at the top of your script)
4. **If the labs are causing major problems or your computer hardware is struggling (or you have any other software issue), Talk to Dr Greatrex**. We can fix this and there are other free/cheap options for using R online.
<br>
# STEP 1: Lab set up
<br>
## 1a. Get set up
IF YOU ARE DOING THIS ON YOUR COMPUTER: First, go and look at your STAT-462 folder on your computer. Make sure that everything looks right (e.g. a single sub-folder for each lab containing your project file, your Rmd and your html, along with any datafiles/pics as needed). If so, congrats! If not, chat to Dr G.
1. Open R-studio. Go to New Project / New Directory / New Project. Then name your project **Lab3-Project**. Place the location inside your STAT-462 folder. If you are stuck, see
[Tutorial 2.1](https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#21_Projects).
<br>
2. Set up your template:
+ IF YOU ARE WORKING ON YOUR OWN COMPUTER: Go onto your computer, find your STAT462 folder and make a copy of your lab template .Rmd file. Copy it into your lab 3 folder and rename as Lab 3 & your PSU-e-mail e.g. *Lab 3-hlg5155*. (you can do this within the R files tab if you wish but probably easier on your computer) <br>>br>
+ IF YOU ARE WORKING ON R-STUDIO CLOUD: In the files tab, there is an upload button. Click that and upload last week's template that you saved onto your computer. Click the checkbox by it and rename to *Lab 3-hlg5155* (with your ID). (If you didn't download a copy of your lab template file, go into your lab 2 project and do so first). <br><br>
3. Go back to R studio, make sure you are running the Lab 3 project, then go to the files quadrant and click on your lab3 script to open. Update the title etc and make any formatting tweaks you like.
<br><br>
## 1c. Load libraries and check it all knits
In the library section of your lab report, add a new code chunk and use this code to load the following libraries. If some don't exist on your computer or on the cloud, use [Tutorial 2.3] (https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#23_Adding_a_new_package) to install/download them first. To make sure they loaded OK, run the code chunk TWICE. The second time any welcome text will disappear unless there are errors.
```{r, eval=FALSE}
library(tidyverse)
library(dplyr)
library(ggpubr)
library(skimr)
library(ggplot2)
library(plotly)
library(ISLR)
library(equatiomatic)
library(olsrr)
library(Stat2Data)
library(readxl)
library(tmap)
```
Finally, press knit to check the html works and it looks like this (with your theme)
```{r, echo=FALSE, fig.align='center'}
knitr::include_graphics("./Figures/Lab3_fig1.png")
```
<br>
<br>
# STEP 2: Code showcase
**Note, you might want to make a second .Rmd file to practice the tutorials, so you can save your practice but only write up what is needed in your report**
<br>
## 2a. Markdown - inline code and equations
The best thing about R-Markdown is that you can set up reports to automatically update even in the text. For example imagine underneath a code chunk you want to write that the mean of the data is 23. Instead of typing the number 23, we can add *inline code* to automatically create it
*Step A:* Work through [Tutorial 4.8, Inline code](https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#48_Inline_code)
Use the tutorial to do the following.
*Question 1:* In the markdown/code showcase section of your lab report, create a hidden code chunk where you find the answer to pi to the power 4, to 4 decimal places. Write a sentence inside your report containing inline code that shares the answer.
<br>
*Question 2:* Conduct a t-test to find out whether the true mean of this data is significantly less than 13. Hide the test itself and simply write out the concluding summary,using inline code and hidden code chunks.
```{r}
testdata <- c(4,6,2,7,8,34,4,65,6,2,3,1,45,5)
```
*Step B:* Equations
It's often good to put professional looking equations into your report. We can do this using the $ symbol and with some internet support.
Rather than read my tutorial, see this one: https://rmd4sci.njtierney.com/math
I tend to use one of the online LateX maths editors to find the exact commands to make equations and then copy/paste the commands into my markdown For example:
- https://latexeditor.lagrida.com/
- https://www.tutorialspoint.com/latex_equation_editor.htm
*Question 3:* Explain what the central limit theorem is and why we might use a T distribution instead of a Normal one. Use the tutorials above to make sure your equations are formatted correctly.
<br>
<br>
<br>
# STEP 3: Data Analysis
NEW TUTORIALS HAVE APPEARED:
SCATTERPLOTS: [Tutorial 7.8](https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#78_Scatterplots), CORRELATION PLOTS/CALCULATIONS [Tutorial 6.3](https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#63_Correlation), REGRESSION [Tutorial 9](https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#9_Regression_models)
First - ARE YOU USING THE TEMPLATE FROM LAB 2??? IF NOT, YOU WILL LOSE MANY MARKS. GO BACK AND DO STEP 1 OF THIS LAB (and step 1 of lab 2 if you missed it..)
OK What are we doing here? The aim of EVERY LAB, is to get comfortable writing up analyses of a complex dataset for a specific person or user. The modelling might be the same and we follow the same steps, but the context and topic will mean you will need to think hard about what is important in each case.
The overall structure of the data anlysis for *every lab* will be the same, but each week we will add on a little more. Specifically
1. Exploring the data IN THE CONTEXT OF THE STUDY (ALL the points/questions in the first part of the teaching notes : https://psu.instructure.com/courses/2174925/files/132549205). The aim of this is to fully parameterise the study, talking about your response, predictors, sample vs census, object of analysis etc..
2. Exploratory data analysis of your RESPONSE VARIABLE, because understanding/predicting the response is literally the only aim of a regression analysis.
3. A series of models. A model could be very simple (nothing ' explains')
## Our study
Next month, your friend is moving to Sindian Dist., in New Taipei City, Taiwan. They want to buy a house and have asked you to figure out what most impacts house price. (this course is normally less house price/penguin heavy! Next week....)
1. Download the "Lab03_house.xlsx" dataset from the Lab page on canvas and put it into your Lab 3 folder. Use the read_excel() command to read it in and save it to a variable called house:
```{r}
# This only works if you are running your project
# If it can't find the file, use file.choose() to locate it, as described in homework 3
# Then add in the full location rather than just the file name.
house <- read_excel("Lab03_house.xlsx")
```
2. Explore the dataset (using summaries etc (lab 1 & 2), and by reading more about the data here: https://archive.ics.uci.edu/ml/datasets/Real+estate+valuation+data+set). THE AIM OF THIS STUDY IS TO HELP YOUR FRIEND. <br><br> So, describe the dataset to your friend. Use all the hints/questions in the teaching notes: https://psu.instructure.com/courses/2174925/files/132549205 For example talking about your response, predictors, sample vs census, object of analysis etc... <br> Make sure to summarise if there any limitations using this specific sample in order to address the aim of the study.
3. Finish your the initial description by using a paragraph/sentence similar to the one about penguins in the teaching notes just before EDA.
3. Adjust/fill in the template to end of Exploratory Data Analysis (EDA), **focusing only on your response variable for now** (because the aim of the study is to understand the response variable..). Here are the teaching notes
Our first model will be univariate - we think nothing impacts house prices, so all we have is the histogram. Assume that a Normal distribution can fit the data, with mean(sample_mean) and sd(sample_sd). Adjust the title of your model section to read
### Model 1: Univariate model: Normal Distribution
4. In model fitting, write a sentence to explain the above, but instead of writing out "mean(sample_mean) and sd(sample_sd)", use your new equation knowledge and inline text to write out the model more formally. We will talk about this in class. e.g. We will assume a Normal distribution with $~N(\mu_y , \sigma_y) $ where $\mu_y = ..$ etc.
5. Under check validity, use a Shapiro-Wilk test, a QQplot and a histogram to assess if you think the house price data really is normally distributed. Note, if you created them above, you can refer back to them in the text. Make sure to fully explain any contradictory results to your friend. Finish by summarising whether you believe that your model is valid given the data and whether it is suitable to use this for either confidence intervals or prediction intervals.
6. Ignore test skill for now (we will need it later in the course)
Your friend is impatient and wants you to use the model straight away
7. Under "Assess errors on population parameters:", calculate using R the 99th confidence interval on the population mean and write up the results in your text in full sentences. E.g. according to the central limit theorem, we can assume.. therefore.. etc.
8. In the same section, my friend has been told that the average price is 350,000 New Taiwan Dollar/Ping (note the data is in units x 10000). Given your sample, conduct a hypothesis test to assess how likely it is to see your sample result if that statement is likely to be true, with a critical significance of 80% *.<br>
*Note, here I say "80%" as in the real world, people often mix and match the way they talk about a critical threshold as either the critical p, or the critical 1-p. This is common outside stats textbooks! So this is adding to the 'real life messiness' learning objective.." <br> It can be frustrating because it's not immediately obvious what to choose as your critical threshold.. But remember that hypothesis tests normally look for an unusual outcome so I normally assume if it's over 50% then they mean 1-p.. Best practice would be to choose either one, but then write a short sentence or two in the report to your friend explaining what the p value means and asking, "did you really mean 80%? because that would mean....*
9. In the predict new value, find the 99% prediction interval for a brand new house. Given this is it very unusual to see a new house priced at over 700,000 New Taiwan Dollar/Ping? (note the data is in units x 10000). (Help including R output in the Lecture 9 notes)
## 3B. Bivariate model
Make a new level 1 section called Bivariate model.
We do know ways to improve our model, as we know things that might impact house prices contained in our data. Specifically, our friend has been told that houses might be more expensive in the North.
10. Scatterplot
See: SCATTERPLOTS: [Tutorial 7.8](https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#78_Scatterplots)
and REGRESSION [Tutorial 9](https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#9_Regression_models)
- Look at the dataset and see which column gives you information about North/South (hint Latitude/longitude, https://www.geographyrealm.com/latitude-longitude/)
- Create a scatterplot to assess this, REMEMBER WHICH IS YOUR RESPONSE!
- Describe the scatterplot fully as discussed in Homework 5 (or see here: [KHAN SUMMARY:](https://www.khanacademy.org/math/ap-statistics/bivariate-data-ap/scatterplots-correlation/a/describing-scatterplots-form-direction-strength-outliers?modal=1) )
- Create a Simple Linear Regression model to assess the issue (Regression tutorial)
- Extract the equation using equatiomatrix (see the tutorial) and write it below the plot, explaining what the intercept and slope are.
- Plot the abline, line of best fit onto a new scatterplot
11. Comment on why this might be misleading as an analysis? To help you answer this question, try running this code to further explore the data on a map.
DONT RUN IF TMAP NOT WORKING
```{r,eval=FALSE}
# Command from the sf library
# ADD THE library(sf) and library(tmap) TO YOUR LIBRARY CODE CHUNK
# Make a spatial version of the data using the Longitude and Latitude columns
house.spatial <- st_as_sf(house,coords=c("Longitude","Latitude"),crs = 4326)
# make interactive, for static set as "plot"
tmap_mode("view")
# Command from the tmap library
# and plot
tm_basemap("Esri.WorldTopoMap") +
qtm(house.spatial, # data
symbols.col="House.Price", # which column for the symbols
symbols.alpha=0.9, # transparency
symbols.size=.2, # how big
symbols.palette="Spectral", #colors from https://colorbrewer2.org
symbols.style="fisher") # color breaks
```
12. Beyond this, what other confounding variables might there be? Is there a variable that is more important than latitude in predicting house prices in your dataset? Provide evidence to justify your answer. To help you answer this data and answer this question, some useful code includes:
+ [A] corrplot(). A quick look at the correlation coefficient between all the variables. We will discuss it more next week. CORRELATION PLOTS/CALCULATIONS [Tutorial 6.3](https://psu-spatial.github.io/stat462-2022/T1_R_Basics.html#63_Correlation),
```{r,eval=FALSE}
library(corrplot)
# ADD THAT LIBRARY TO YOUR LIST!
corrplot(cor(house),method="number",type="lower")
```
+ [B] If you then want to look at three variables together, you can use an interactive plot
e.g you can use this code and change the response, y (currently house price), the predictor x (currently latitude) and the colour (currently house.age).
```{r, eval=FALSE}
# Create a plot
p <- house %>%
ggplot( aes(Latitude,House.Price, col= House.Age)) +
geom_point() +
theme_classic()+
scale_color_gradient(low="blue", high="red")
# and actually plot it
ggplotly(p)
```
<br>
<br>
# STEP 4: Show me something new
Remember that an A is 94%, so you can ignore this section and still easily get an A. But here is your time to shine. Also, if you are struggling in another part of the lab, you can use this to gain back points.
**To get the final 4 marks in the lab, you need to show me something new, e.g. you need to go above and beyond the lab questions in some way.**
- You get 2/4 for doing something new in any way BEYOND THE LAB INSTRUCTIONS/CODE
- You get 4/4 for something really impressive or multiple small things.
- You must tell us in your R script what you did!
Here are some ideas:
- You found a cool new plot from R graph gallery or flowingdata.com and made it work
- You did something neat in Markdown such as embedding a pdf
- You found a new package/command that did something relevant
- You found something cool on data camp and applied it here
- You tweaked your mapping/plotting code to be better
<br>
<br>
# Submitting your Lab
Remember to save your work throughout and to spell check your writing (next to the save button).
Now, press the knit button for the final time.
If you have not made any mistakes in the code then R should create a html file in your lab 2 folder which includes your answers. If you look at your lab 1 folder, you should see this there - complete with a very recent time-stamp.
In that folder, double click on the html file. This will open it in your browser. CHECK THAT THIS IS WHAT YOU WANT TO SUBMIT.
If you are on R studio cloud, see Tutorial 1 for how to download your files
Now go to Canvas and submit BOTH your html and your .Rmd file in Lab 2.
```{r, echo=FALSE}
knitr::include_graphics("./Figures/Lab1_Basics_2022_fig1.png")
```
<br>
# Grading Rubric/checklist
See the table below for what this means - 100% is hard to get!
**HTML FILE SUBMISSION - 8 marks**
**RMD CODE SUBMISSION - 8 marks**
**WRITING/CODE STYLE - 10 MARKS**
Your code and document is neat and easy to read. LOOK AT YOUR HTML FILE IN YOUR WEB-BROWSER BEFORE YOU SUBMIT. There is also a spell check next to the save button.
You have written your answers below the relevant code chunk in full sentences in a way that is easy to find and grade. For example, you have written in full sentences, it is clear what your answers are referring to. You have used units and explained your workings.
**MARKDOWN SHOWCASE: 10 MARKS**
You use full sentences and units, You have great Markdown formatting
**R-CODE SHOWCASE: 20 MARKS**
**UNIVARIATE: 20 MARKS**
**BIVARIATE: 20 MARKS**
You have managed to successfully complete all the code challenges
**Above and beyond: 4 MARKS**
See above for ideas on grading
[100 marks total]
Overall, here is what your lab should correspond to:
```{r, echo=FALSE}
rubric <- readxl::read_excel("STAT462_22_LRubric.xlsx")
knitr::kable(rubric) %>%
kable_classic_2() %>%
kable_styling(bootstrap_options = c("striped", "hover", "responsive"))
```