Skip to content

Commit a0fcea5

Browse files
authored
Merge branch 'dev' into patch-fig-floating
2 parents 3ceb826 + dad07d8 commit a0fcea5

File tree

7 files changed

+116
-36
lines changed

7 files changed

+116
-36
lines changed

classification1.Rmd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ knitr::opts_chunk$set(echo = TRUE,
1111
options(knitr.table.format = function() {
1212
if (knitr::is_latex_output()) 'latex' else 'pandoc'
1313
})
14+
reticulate::use_miniconda('r-reticulate')
1415
```
1516

1617
## Overview
@@ -572,7 +573,7 @@ Based on $K=5$ nearest neighbors with these three predictors we would classify t
572573
Figure \@ref(fig:05-more) shows what the data look like when we visualize them
573574
as a 3-dimensional scatter with lines from the new observation to its five nearest neighbors.
574575

575-
```{r 05-more, echo = FALSE, message = FALSE, fig.cap = "3D scatter plot of the standardized symmetry, concavity, and perimeter variables. Note that in general we recommend against using 3D visualizations; here we show the data in 3D only to illustrate what higher dimensions and nearest neighbors look like, for learning purposes.", fig.retina=2, out.width="80%"}
576+
```{r 05-more, echo = FALSE, message = FALSE, fig.cap = "3D scatter plot of the standardized symmetry, concavity, and perimeter variables. Note that in general we recommend against using 3D visualizations; here we show the data in 3D only to illustrate what higher dimensions and nearest neighbors look like, for learning purposes.", fig.retina=2, out.width="100%"}
576577
attrs <- c("Perimeter", "Concavity", "Symmetry")
577578
578579
# create new scaled obs and get NNs
@@ -602,7 +603,7 @@ plot_3d <- scaled_cancer_3 |>
602603
z = ~Symmetry,
603604
color = ~Class,
604605
opacity = 0.4,
605-
size = 150,
606+
size = 2,
606607
colors = c("orange2", "steelblue2", "red"),
607608
symbol = ~Class, symbols = c('circle','circle','diamond'))
608609
@@ -641,6 +642,11 @@ plot_3d <- plot_3d %>%
641642
if(!is_latex_output()){
642643
plot_3d
643644
} else {
645+
# scene = list(camera = list(eye = list(x=2, y=2, z = 1.5)))
646+
# plot_3d <- plot_3d %>% layout(scene = scene)
647+
# save_image(plot_3d, "img/plot3d_knn_classification.png", scale = 10)
648+
# cannot adjust size of points in this plot for pdf
649+
# so using a screenshot for now instead
644650
knitr::include_graphics("img/plot3d_knn_classification.png")
645651
}
646652
```

img/generate-pat_01.png

120 KB
Loading

img/generate-pat_02.png

402 KB
Loading

img/generate-pat_03.png

202 KB
Loading

regression1.Rmd

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ library(knitr)
55
library(plotly)
66
77
knitr::opts_chunk$set(fig.align = "center")
8+
reticulate::use_miniconda('r-reticulate')
89
```
910

1011
## Overview
@@ -759,7 +760,7 @@ Figure \@ref(fig:07-knn-mult-viz) visualizes the model's predictions overlaid on
759760
time the predictions are a surface in 3D space, instead of a line in 2D space, as we have 2
760761
predictors instead of 1.
761762

762-
```{r 07-knn-mult-viz, echo = FALSE, message = FALSE, warning = FALSE, fig.cap = "KNN regression model’s predictions represented as a surface in 3D space overlaid on top of the data using three predictors (price, house size, and the number of bedrooms). Note that in general we recommend against using 3D visualizations; here we use a 3D visualization only to illustrate what the surface of predictions looks like for learning purposes.", out.width="80%"}
763+
```{r 07-knn-mult-viz, echo = FALSE, message = FALSE, warning = FALSE, fig.cap = "KNN regression model’s predictions represented as a surface in 3D space overlaid on top of the data using three predictors (price, house size, and the number of bedrooms). Note that in general we recommend against using 3D visualizations; here we use a 3D visualization only to illustrate what the surface of predictions looks like for learning purposes.", out.width="100%"}
763764
xvals <- seq(from = min(sacramento_train$sqft),
764765
to = max(sacramento_train$sqft),
765766
length = 50)
@@ -780,12 +781,12 @@ plot_3d <- plot_ly() |>
780781
x = ~sqft,
781782
y = ~beds,
782783
z = ~price,
783-
marker = list(size = 5, opacity = 0.4, color = "red")
784+
marker = list(size = 2, opacity = 0.4, color = "red")
784785
) |>
785786
layout(scene = list(
786-
xaxis = list(title = "House size (square feet)"),
787+
xaxis = list(title = "Size (sq ft)"),
787788
zaxis = list(title = "Price (USD)"),
788-
yaxis = list(title = "Number of bedrooms")
789+
yaxis = list(title = "Bedrooms")
789790
)) |>
790791
add_surface(
791792
x = ~xvals,
@@ -797,6 +798,9 @@ plot_3d <- plot_ly() |>
797798
if(!is_latex_output()){
798799
plot_3d
799800
} else {
801+
scene = list(camera = list(eye = list(x = -2.1, y = -2.2, z = 0.75)))
802+
plot_3d <- plot_3d |> layout(scene = scene)
803+
save_image(plot_3d, "img/plot3d_knn_regression.png", scale = 10)
800804
knitr::include_graphics("img/plot3d_knn_regression.png")
801805
}
802806
```

regression2.Rmd

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ library(knitr)
55
library(plotly)
66
77
knitr::opts_chunk$set(fig.align = "center")
8+
reticulate::use_miniconda('r-reticulate')
89
```
910

1011
## Overview
@@ -453,7 +454,7 @@ is `r format(round(lm_mult_test_results %>% filter(.metric == 'rmse') %>% pull(.
453454
In the case of two predictors, we can plot the predictions made by our linear regression creates a *plane* of best fit, as
454455
shown in Figure \@ref(fig:08-3DlinReg).
455456

456-
```{r 08-3DlinReg, echo = FALSE, message = FALSE, warning = FALSE, fig.cap = "Linear regression plane of best fit overlaid on top of the data (using price, house size, and number of bedrooms as predictors). Note that in general we recommend against using 3D visualizations; here we use a 3D visualization only to illustrate what the regression plane looks like for learning purposes.", out.width="80%"}
457+
```{r 08-3DlinReg, echo = FALSE, message = FALSE, warning = FALSE, fig.cap = "Linear regression plane of best fit overlaid on top of the data (using price, house size, and number of bedrooms as predictors). Note that in general we recommend against using 3D visualizations; here we use a 3D visualization only to illustrate what the regression plane looks like for learning purposes.", out.width="100%"}
457458
xvals <- seq(from = min(sacramento_train$sqft),
458459
to = max(sacramento_train$sqft),
459460
length = 50)
@@ -474,12 +475,12 @@ plot_3d <- plot_ly() |>
474475
x = ~sqft,
475476
y = ~beds,
476477
z = ~price,
477-
marker = list(size = 5, opacity = 0.4, color = "red")
478+
marker = list(size = 2, opacity = 0.4, color = "red")
478479
) |>
479480
layout(scene = list(
480-
xaxis = list(title = "House size (square feet)"),
481+
xaxis = list(title = "Size (sq ft)"),
481482
zaxis = list(title = "Price (USD)"),
482-
yaxis = list(title = "Number of bedrooms")
483+
yaxis = list(title = "Bedrooms")
483484
)) |>
484485
add_surface(
485486
x = ~xvals,
@@ -491,6 +492,9 @@ plot_3d <- plot_ly() |>
491492
if(!is_latex_output()){
492493
plot_3d
493494
} else {
495+
scene = list(camera = list(eye = list(x = -2.1, y = -2.2, z = 0.75)))
496+
plot_3d <- plot_3d %>% layout(scene = scene)
497+
save_image(plot_3d, "img/plot3d_linear_regression.png", scale = 10)
494498
knitr::include_graphics("img/plot3d_linear_regression.png")
495499
}
496500
```

version-control.Rmd

Lines changed: 92 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ version control: you won't be able to see, interpret, or find changes in the his
468468
of any of the actual content of your project!
469469

470470
## Working with local repositories using Jupyter {#local-repo-jupyter}
471+
471472
Although there are several ways to create and edit files on GitHub, they are
472473
not quite powerful enough for efficiently creating and editing complex files,
473474
or files that need to be executed to assess whether they work (e.g., files
@@ -477,14 +478,76 @@ remote repository that was created on GitHub to a local coding environment. Thi
477478
can be done by creating and working in a local copy of the repository.
478479
In this chapter, we focus on interacting with Git via Jupyter using
479480
the Jupyter Git extension. The Jupyter Git \index{git!Jupyter extension} extension
480-
can be run via Jupyter on your local computer, or on a JupyterHub server.
481+
can be run by Jupyter on your local computer, or on a JupyterHub server.
481482
*Note: we recommend reading Chapter \@ref(getting-started-with-jupyter)*
482483
*to learn how to use Jupyter before reading this chapter.*
483484

485+
### Generating a GitHub personal access token
486+
487+
To send and retrieve work between your local repository
488+
and the remote repository on GitHub,
489+
you will frequently need to authenticate with GitHub
490+
to prove you have the required permission.
491+
There are several methods to do this,
492+
but for beginners we recommend using the HTTPS method
493+
because it is easier and requires less setup.
494+
In order to use the HTTPS method,
495+
GitHub requires you to provide a *personal access token*. \index{GitHub!personal access token}
496+
A personal access token is like a password&mdash;so keep it a secret!&mdash;but it gives
497+
you more fine-grained control over what parts of your account
498+
the token can be used to access, and lets you set an expiry date for the authentication.
499+
To generate a personal access token,
500+
you must first visit [https://github.com/settings/tokens](https://github.com/settings/tokens),
501+
which will take you to the "Personal access tokens" page in your account settings.
502+
Once there, click "Generate new token" (Figure \@ref(fig:generate-pat-01)).
503+
Note that you may be asked to re-authenticate with your username
504+
and password to proceed.
505+
506+
(ref:generate-pat-01) The "Generate new token" button used to initiate the creation of a new personal access token. It is found in the "Personal access tokens" section of the "Developer settings" page in your account settings.
507+
508+
```{r generate-pat-01, fig.cap = '(ref:generate-pat-01)', fig.retina = 2, out.width="100%"}
509+
image_read("img/generate-pat_01.png")
510+
```
511+
512+
Next you will be asked to add a note for your personal access token.
513+
This should be a descriptor of the token's purpose.
514+
Finally, you need to select permissions for the token; this is where
515+
you can control what parts of your account the token can be used to access.
516+
Make sure to choose only those permissions that you absolutely require. In
517+
Figure \@ref(fig:generate-pat-02), we tick only the "repo" box, which gives the
518+
token access to our repositories (so that we can push and pull) but none of our other GitHub
519+
account features. Finally, to generate the token, scroll to the bottom of that page
520+
and click the green "Generate token" button (Figure \@ref(fig:generate-pat-02)).
521+
522+
(ref:generate-pat-02) Webpage for creating a new personal access token.
523+
524+
```{r generate-pat-02, fig.cap = '(ref:generate-pat-02)', fig.retina = 2, out.width="100%"}
525+
image_read("img/generate-pat_02.png")
526+
```
527+
528+
Finally, you will be taken to a page where you will be able to see
529+
and copy the personal access token you just generated (Figure \@ref(fig:generate-pat-03)).
530+
Since it provides access to certain parts of your account, you should
531+
treat this token like a password; for example, you should consider
532+
securely storing it (and your other passwords and tokens, too!) using a password manager.
533+
Note that this page will only display the token to you once,
534+
so make sure you store it in a safe place right away. If you accidentally forget to
535+
store it, though, do not fret&mdash;you can delete that token by clicking the
536+
"Delete" button next to your token, and generate a new one from scratch.
537+
To learn more about GitHub authentication,
538+
see the additional resources section at the end of this chapter.
539+
540+
(ref:generate-pat-03) Display of the newly generated personal access token.
541+
542+
```{r generate-pat-03, fig.cap = '(ref:generate-pat-03)', fig.retina = 2, out.width="100%"}
543+
image_read("img/generate-pat_03.png")
544+
```
545+
484546
### Cloning a repository using Jupyter
485547

486-
The first step is to *clone* the \index{git!clone} remote repository on GitHub to create a local repository,
487-
i.e., make a
548+
Now that we have everything we need for authentication,
549+
the next step is to *clone* the \index{git!clone} remote repository on GitHub
550+
to create a local repository, i.e., make a
488551
copy that knows where it was obtained from so that it knows where send/receive
489552
new committed edits. In order to do this, first copy the URL from the HTTPS tab
490553
of the Code drop down menu on GitHub (Figure \@ref(fig:clone-02)).
@@ -521,9 +584,9 @@ image_read("img/version_control/clone_04.png") %>%
521584
```
522585

523586
### Specifying files to commit
524-
Now that we have cloned the remote repository from GitHub to create a local repository,
525-
you can get to work editing, creating, and deleting files.
526-
For example, suppose we created and saved a new file (named `eda.ipynb`) that we would
587+
Now that you have cloned the remote repository from GitHub to create a local repository,
588+
you can get to work editing, creating, and deleting files.
589+
For example, suppose you created and saved a new file (named `eda.ipynb`) that you would
527590
like to send back to the project repository on GitHub (Figure \@ref(fig:git-add-01)).
528591
To "add" this modified file to the staging area (i.e., flag that this is a
529592
file whose changes we would like to commit), click the Jupyter Git extension
@@ -537,8 +600,8 @@ This opens the Jupyter Git graphical user interface pane. Next,
537600
click the plus sign (+) beside the file(s) that you want to "add" \index{git!add}
538601
(Figure \@ref(fig:git-add-02)). Note that because this is the
539602
first change for this file, it falls under the "Untracked" heading.
540-
However, next time we edit this file and want to add the changes,
541-
we will find it under the "Changed" heading.
603+
However, next time you edit this file and want to add the changes,
604+
you will find it under the "Changed" heading.
542605

543606
You will also see an `eda-checkpoint.ipynb` file under the "Untracked" heading.
544607
This is a temporary "checkpoint file" created by Jupyter when you work on `eda.ipynb`.
@@ -552,9 +615,9 @@ image_read("img/version_control/git_add_02.png")
552615
```
553616

554617
Clicking the plus sign (+) moves the file from the "Untracked" heading to the "Staged" heading,
555-
flagging this file so that Git knows we want a snapshot of its current state
618+
flagging this file so that Git knows you want a snapshot of its current state
556619
as a commit (Figure \@ref(fig:git-add-03)).
557-
Now we are ready to "commit" the changes.
620+
Now you are ready to "commit" the changes.
558621
Make sure to include a (clear and helpful!) message about what was changed
559622
so that your collaborators (and future you) know what happened in this commit.
560623

@@ -567,13 +630,13 @@ image_read("img/version_control/git_add_03.png")
567630
### Making the commit
568631

569632
To snapshot the changes with an associated commit message,
570-
we put a message in the text box at the bottom of the Git pane
633+
you must put a message in the text box at the bottom of the Git pane
571634
and click on the blue "Commit" button (Figure \@ref(fig:git-commit-01)). \index{git!commit}
572635
It is highly recommended to write useful and meaningful messages about what
573636
was changed. These commit messages, and the datetime stamp for a given
574637
commit, are the primary means to navigate through the project's history in the
575-
event that we need to view or retrieve a past version of a file, or
576-
revert our project to an earlier state.
638+
event that you need to view or retrieve a past version of a file, or
639+
revert your project to an earlier state.
577640
When you click the "Commit" button for the first time, you will be prompted to
578641
enter your name and email. This only needs to be done once for each machine
579642
you use Git on.
@@ -582,9 +645,9 @@ you use Git on.
582645
image_read("img/version_control/git_commit_01.png")
583646
```
584647

585-
After "committing" the file(s), you will see there are 0 "Staged" files
586-
and we are now ready to push our changes
587-
to our remote repository on GitHub (Figure \@ref(fig:git-commit-03)).
648+
After "committing" the file(s), you will see there are 0 "Staged" files.
649+
You are now ready to push your changes
650+
to the remote repository on GitHub (Figure \@ref(fig:git-commit-03)).
588651

589652
```{r git-commit-03, fig.cap = 'After recording a commit, the staging area should be empty.', fig.retina = 2, out.width="100%"}
590653
image_read("img/version_control/git_commit_03.png")
@@ -593,7 +656,8 @@ image_read("img/version_control/git_commit_03.png")
593656
### Pushing the commits to GitHub
594657

595658
To send the committed changes back to the remote repository on
596-
GitHub, we need to *push* them. \index{git!push} To do this we click on the cloud icon with the up arrow on the Jupyter Git tab
659+
GitHub, you need to *push* them. \index{git!push} To do this,
660+
click on the cloud icon with the up arrow on the Jupyter Git tab
597661
(Figure \@ref(fig:git-push-01)).
598662

599663
(ref:git-push-01) The Jupyter Git extension "push" button (circled in red).
@@ -602,23 +666,25 @@ GitHub, we need to *push* them. \index{git!push} To do this we click on the clou
602666
image_read("img/version_control/git_push_01.png")
603667
```
604668

605-
We will then be prompted to enter our GitHub username
606-
and password, and click the blue "OK" button (Figure \@ref(fig:git-push-02)).
669+
You will then be prompted to enter your GitHub username
670+
and the personal access token that you generated
671+
earlier (not your account password!). Click
672+
the blue "OK" button to initiate the push (Figure \@ref(fig:git-push-02)).
607673

608674
```{r git-push-02, fig.cap = 'Enter your Git credentials to authorize the push to the remote repository.', fig.retina = 2, out.width="100%"}
609675
image_read("img/version_control/git_push_02.png")
610676
```
611677

612-
If the files were successfully pushed to our project repository on
613-
GitHub we will be given the success message shown below.
614-
Click "Dismiss" to continue working in Jupyter (Figure \@ref(fig:git-push-03)).
678+
If the files were successfully pushed to the project repository on
679+
GitHub, you will be shown a success message (Figure \@ref(fig:git-push-03)).
680+
Click "Dismiss" to continue working in Jupyter.
615681

616682
```{r git-push-03, fig.cap = 'The prompt that the push was successful.', fig.retina = 2, out.width="100%"}
617683
image_read("img/version_control/git_push_03.png")
618684
```
619685

620686
If you visit the remote repository on GitHub,
621-
you will see that the changes now exist there via their web interface
687+
you will see that the changes now exist there too
622688
(Figure \@ref(fig:git-push-04))!
623689

624690
```{r git-push-04, fig.cap = 'The GitHub web interface shows a preview of the commit message, and the time of the most recently pushed commit for each file.', fig.retina = 2, out.width="100%"}
@@ -756,7 +822,7 @@ image_read("img/version_control/merge_conflict_03.png") %>%
756822

757823
### Handling merge conflicts
758824

759-
To fix the merge conflict, \index{git!merge conflict} we need to open the offending file
825+
To fix the merge conflict, \index{git!merge conflict} you need to open the offending file
760826
in a plain text editor and look for special marks that Git puts in the file to
761827
tell you where the merge conflict occurred (Figure \@ref(fig:merge-conflict-04)).
762828

@@ -865,7 +931,7 @@ Now that you've picked up the basics of version control with Git and GitHub,
865931
you can expand your knowledge through the resources listed below:
866932

867933
- GitHub's [guides website](https://guides.github.com/) and [YouTube channel](https://www.youtube.com/githubguides),
868-
and [Happy Git](https://happygitwithr.com/) are great resources to take the next steps in learning about Git and GitHub.
934+
and [*Happy Git with R*](https://happygitwithr.com/) are great resources to take the next steps in learning about Git and GitHub.
869935
- [Good enough practices in scientific computing](https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1005510#sec014) [-@wilson2014best] provides more advice on useful workflows and "good enough" practices in data analysis projects.
870936
- In addition to [GitHub](https://github.com), there are other popular Git repository hosting services such as [GitLab](https://gitlab.com) and [BitBucket](https://bitbucket.org). Comparing all of these options is beyond the scope of this book, and until you become a more advanced user you are perfectly fine to just stick with GitHub. Just be aware that you have options!
871-
937+
- [GitHub's documentation on creating a personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) and the *Happy Git with R* [cache credentials for HTTPS](https://happygitwithr.com/credential-caching.html) chapter are both excellent additional resources to consult if you need additional help generating and using personal access tokens.

0 commit comments

Comments
 (0)