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

Update ComponentModel.Rmd

parent 54fb308c
---
title: "R Notebook"
title: "Component model"
output:
html_notebook: default
html_document: default
---
#Import data
```{r}
library(readxl)
Component_Data <- read_excel("ComponentData_Average_data.xlsx",
sheet = "7. Training_set_SFSP") #Fill in the category to be analysed: Isolates, Mildly refined, Mixtures, Alldata
......@@ -14,21 +12,18 @@ attach(Component_Data)
#detach(Component_Data)
```
```{r}
#rename variables
```{r}
starch = `Starch (%)`
protein = `Protein (%)`
fibre = `Fibre (%)`
rest = `Residual components`
viscosity= log(`Final viscosity (mPa.s)`)
```
## Load libraries
```{r}
library(MASS)
library(tidyverse)
library(ggplot2)
......@@ -43,7 +38,6 @@ library(readxl)
```
# Create all possible formulas
```{r}
terms <- c("protein", "starch", "fibre", "rest",
......@@ -59,7 +53,6 @@ right_formulas <- lapply(model_terms, function(x) {
```
#Fitting the models with multiple linear regression
```{r}
Viscosity_formulas <- lapply(right_formulas, function(x) {
lapply(x, function(y) paste0("viscosity ~ ", y))
......@@ -73,7 +66,6 @@ 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)
......@@ -103,12 +95,10 @@ 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)))
......@@ -130,7 +120,6 @@ AIC_arranged
```
# Print the summary of the 5 best models by AIC.
```{r}
print_models_AIC <- lapply(1:2, function(x) {
......@@ -144,7 +133,6 @@ HTML2clip(print_models_AIC) #to copy results to clipboard
```
# Print 5 best models according to the adjusted $R^2$.
```{r}
#print_models_R2 <-
lapply(1:5, function(x) {
......@@ -155,11 +143,9 @@ lapply(1:5, function(x) {
})
#HTML2clip(print_models_R2) #to copy results to clipboard
```
# 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]]]
......@@ -188,7 +174,6 @@ lapply(1:1, function(x) {
```
#Plot parity plot
```{r}
lapply(1:1, function(x){
my_models <- Viscosity_models[[AIC_arranged$n_terms[x]]]
models_AIC <- lapply(my_models, AIC)
......@@ -205,7 +190,7 @@ lapply(1:1, function(x){
print(p1)
})
```
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