generated from sib-swiss/course_website_template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jeitziner
committed
Oct 18, 2022
1 parent
fd8b4d1
commit 0ba8c84
Showing
29 changed files
with
34 additions
and
121 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1 @@ | ||
## **Bonus code** :champagne_glass: | ||
|
||
The following code was added thanks to questions from course participants of past sessions. They might be useful for you too. | ||
|
||
[Download slides](assets/pdf/Regularization.pdf){: .md-button } | ||
|
||
## Linear Regression using model selection | ||
|
||
### Linear regression | ||
|
||
```r | ||
library(MASS) | ||
|
||
data(birthwt) | ||
summary(birthwt) | ||
|
||
help(birthwt) | ||
|
||
colnames(birthwt) | ||
colnames(birthwt) <- c("birthwt.below.2500", "mother.age","mother.weight", "race", | ||
"smoking.status", "nb.previous.prem.labor", "hypertension", | ||
"uterine.irrit","nb.physician.visits", "birthwt.grams") | ||
|
||
str(birthwt) | ||
summary(birthwt) | ||
birthwt$race <- as.factor(birthwt$race) | ||
str(birthwt) | ||
summary(birthwt) | ||
``` | ||
|
||
|
||
|
||
### Model selection | ||
|
||
```r | ||
library(leaps) | ||
|
||
best_subset <- regsubsets(birthwt.grams ~ . - birthwt.below.2500, data = birthwt, nvmax = 8) | ||
results <- summary(best_subset) | ||
|
||
# Adjusted R-squared | ||
plot(results$adjr2, xlab = "Number of Variables", ylab = "Adjusted R-squared", type = "l") | ||
# Residual sum of squares for each model | ||
plot(results$rss, xlab = "Number of Variables", ylab = "RSS", type = "l") | ||
# R-squared | ||
plot(results$rsq, xlab = "Number of Variables", ylab = "R-squared", type = "l") | ||
|
||
which.max(results$adjr2) | ||
``` | ||
|
||
|
||
### Model selection using the validation set approach | ||
|
||
```r | ||
set.seed(1) | ||
train <- sample(c(TRUE, FALSE), size = nrow(birthwt), rep = TRUE) | ||
test <- (!train) | ||
|
||
best_subset_train <- regsubsets(birthwt.grams ~ . - birthwt.below.2500, data = birthwt[train ,], nvmax = 8) | ||
test_mat <- model.matrix(birthwt.grams ~ . - birthwt.below.2500, data = birthwt[test,]) | ||
|
||
val_errors <- rep(NA , 8) | ||
for(i in 1:8){ | ||
coefi = coef(best_subset_train, id = i) | ||
pred = test_mat[,names(coefi)]%*%coefi | ||
val_errors[i] = mean((birthwt$birthwt.grams[test] - pred)^2) | ||
} | ||
which.min(val_errors) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
## **EXAM** :scream: | ||
|
||
The participants who need credits must answer the following questions and send the results as an R script with comments to [email protected] until latest friday 2nd of September 2022. | ||
The participants who need credits must answer the following questions and send the results as an R script with comments to [email protected] until latest February 2023. | ||
|
||
Data: A set of data collected by Heinz et al.(* Heinz G, Peterson LJ, Johnson RW, Kerk CJ Journal of Statistics Education Volume 11, Number 2 (2003) | ||
jse.amstat.org/v11n2/datasets.heinz.html | ||
Copyright © 2003 by Grete Heinz, Louis J. Peterson, Roger W. Johnson, and Carter J. Kerk, all rights reserved) is available in the file IS_23_exam.csv | ||
jse.amstat.org/v11n2/datasets.heinz.html, by Grete Heinz, Louis J. Peterson, Roger W. Johnson, and Carter J. Kerk, all rights reserved) is available in the file IS_23_exam.csv | ||
|
||
|
||
Goals: Get to know the overall structure of the data. Summarize variables numerically and graphically. Model relationships between variables. | ||
|
||
[Download exercise material](assets/Exercises_IS/IS_23_exam.csv){: .md-button } | ||
[Download exercise material](assets/exercises/IS_23_exam.csv){: .md-button } | ||
|
||
## Observations | ||
1. Have look at the file in a text editor to get familiar with it | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters