Commit b3be8280 authored by Lie-Piang, Anouk's avatar Lie-Piang, Anouk
Browse files

Update ComponentModel_intercept_ln0.Rmd

parent 37dca944
---
title: "R Notebook"
title: "Component Model intercept ln(0)"
output:
html_notebook: default
html_document: default
---
---
#Import data
```{r}
......@@ -14,9 +13,8 @@ attach(Component_Data)
#detach(Component_Data)
```
```{r}
#rename variables
```{r}
starch = `Starch (%)`
protein = `Protein (%)`
fibre = `Fibre (%)`
......@@ -25,10 +23,8 @@ viscosity= log(`Final viscosity (mPa.s)`)
```
## Load libraries
```{r}
library(MASS)
library(tidyverse)
library(ggplot2)
......@@ -38,14 +34,10 @@ library(cowplot)
library(car)
library(R2HTML)
library(readxl)
```
# Create all possible formulas
```{r}
terms <- c("protein", "starch", "fibre", "rest",
"protein:starch", "protein:fibre", "starch:fibre",
"protein:starch:fibre")
......@@ -57,13 +49,13 @@ right_formulas <- lapply(model_terms, function(x) {
})
```
#Adapt data so intercept can be set through 1.
```{r}
Component_Data$visc_m_1 <- viscosity - 1
```
#Fitting the models with multiple linear regression
```{r}
Viscosity_formulas <- lapply(right_formulas, function(x) {
lapply(x, function(y) paste0("visc_m_1 ~ 0 +", y))
......@@ -76,9 +68,7 @@ Viscosity_models <- lapply(Viscosity_formulas, function(x) {
})
```
# Extract the AICs and the adjusted $R^2 to see which model is the best
```{r}
Viscosity_AIC <- lapply(Viscosity_models, function(x) {
lapply(x, AIC)
......@@ -108,12 +98,9 @@ Viscosity_quality <- lapply(1:length(Viscosity_R2), function(x) {
}) %>%
do.call(rbind.data.frame, .) %>%
cbind(Viscosity_quality, .)
```
#Aikaike versus R2
```{r}
ggplot(Viscosity_quality) +
geom_point(aes(x = R.squared, y = AIC, colour = as.factor(n_terms)))
......@@ -135,7 +122,6 @@ AIC_arranged
```
# Print the summary of the 5 best models by AIC.
```{r}
print_models_AIC <- lapply(1:2, function(x) {
......@@ -149,7 +135,6 @@ HTML2clip(print_models_AIC) #to copy results to clipboard
```
# Print 5 best models according to the adjusted $R^2$.
```{r}
lapply(1:5, function(x) {
my_models <- Viscosity_models[[R2_arranged$n_terms[x]]]
......@@ -163,7 +148,6 @@ lapply(1:5, function(x) {
```
# Residuals vs fitted values for the top 5 models according to AIC.
```{r}
lapply(1:1, function(x) {
my_models <- Viscosity_models[[AIC_arranged$n_terms[x]]]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment