Tools/rstudio

From Open Food Facts wiki
Revision as of 13:41, 31 January 2025 by Aleene (talk | contribs) (→‎Steps)
Jump to navigation Jump to search

Rstudio is a tool do do statistical analysis on data.

Recipe

The tool can be used to find the recipe of products.

Steps

We use the order as found on packaging, i.e. fat, sat. fat, carbs, sugar, fiber, proteins, salt).

Create the product data, e.g:

product <- c(58.0, 6.4, 6.4, 4.2, 8.5, 0.01)

Create the individual ingredients, eg.:

walnuts <- c(67.3, 6.45, 6.88, 3, 6.7, 13.3, 0.1)

Create the ingredients, e.g.:

ingredients <- c(walnuts, hazelnuts, cashews, almonds)

Create the array for 4 ingredients, e.g.:

Z <-array(ingredients, dim=c(7,4))

Fit:

lsFit <- lm(y~Z)

Fit with no intercept:

lsFitZero <- lm(y~Z +0)

Fit with no negative coefficients:

nnFit <- nnls(Z,y)

GLMN-fit

glmnetFit <- glmnet(Z,y, lambda = 0, lower.limit = 0, intercept = FALSE)