Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Set input files
- nutfile = "nutrition.txt"
- weifile = "weight.txt"
- #Ser smoothing parameters (from 0 to 2)
- smoothparam <- 1.1
- smoothparammacros <- 0.6
- #Load libraries
- require("plyr")
- #Read data files
- #Tab-delimited text files
- #
- #nutrition file
- #accepts multiple meals per day
- #
- #columns:
- #Date (dd/mm/yyyy)
- #Calories (number)
- #Fat (numeric, grams)
- #Carbohydrates (numeric, grams)
- #Protein (numeric, grams)
- #
- #weight file
- #accepts multiple measurements per day
- #
- #columns:
- #Date (dd/mm/yyyy)
- #Body.Fat.. (body fat %)
- #Weight (body mass, kilograms)
- nut <- read.table(nutfile, header = T, sep = "\t")
- wei <- read.table(weifile, header = T, sep = "\t")
- #Sum up all meals in each day
- nutd <- ddply(nut,
- .(Date),
- summarise,
- Calories = sum(Calories),
- Fat = sum(Fat),
- Carbohydrates = sum(Carbohydrates),
- Protein = sum(Protein),
- PFat = 100 * (sum(Fat) * 9)/sum(Calories),
- PCarbohydrates = 100 * (sum(Carbohydrates) * 4)/sum(Calories),
- PProtein = 100 * (sum(Protein) * 4)/sum(Calories),
- .progress = "text")
- #Convert date to date format
- nutd$RDate <- as.Date(as.character(nutd$Date), "%d/%m/%Y")
- #order by date
- nutd <- nutd[order(nutd$RDate), ]
- #calculate number of days
- nutd$Days <- as.numeric(nutd$RDate - nutd$RDate[1])
- #remove rows with missing values
- nutd <- nutd[complete.cases(nutd), ]
- #Average multiple weight/bf measurements in each day
- weid <- ddply(wei,
- .(Date),
- summarise,
- Weight = mean(Weight),
- Body.Fat = mean(Body.Fat..),
- .progress = "text")
- #Convert date to date format
- weid$RDate <- as.Date(as.character(weid$Date), "%d/%m/%Y")
- #order by date
- weid <- weid[order(weid$RDate), ]
- #calculate number of days
- weid$Days <- as.numeric(weid$RDate - weid$RDate[1])
- #remove rows with missing values
- weid <- weid[complete.cases(weid), ]
- #calculate the max number of days and create day sequence
- newt <- max(nutd$Days, weid$Days)
- newdays <- c(1:newt)
- #fit smoothing spline to calories
- MCalories <- smooth.spline(nutd$Days, nutd$Calories, spar = smoothparam)
- Caloriesmod <- predict(MCalories, nutd$Days)
- nutd$Caloriesmod <- Caloriesmod$y
- #fit smoothing spline to body mass and calculate deficit
- MWeight <- smooth.spline(weid$Days, weid$Weight, spar = smoothparam)
- Weightmod <- predict(MWeight, newdays)
- WeightmodD <- predict(MWeight, weid$Days, deriv = 1)
- #7700kcal per kilo of fat
- weid$def <- WeightmodD$y * 7700
- #calculate TDEE for all days
- completecal <- predict(MCalories, newdays)$y
- completedef <- predict(MWeight, newdays, deriv = 1)$y * 7700
- TDEE <- completecal - completedef
- #Fit body fat spline
- MBody.Fat <- smooth.spline(weid$Days, weid$Body.Fat, spar = smoothparam)
- BFmod <- predict(MBody.Fat, newdays)
- #Fit nutrient splines
- MFat <- smooth.spline(nutd$Days, nutd$PFat, spar = smoothparammacros)
- MCarb <- smooth.spline(nutd$Days, nutd$PCarbohydrates, spar = smoothparammacros)
- MProt <- smooth.spline(nutd$Days, nutd$PProtein, spar = smoothparammacros)
- Fatmod <- predict(MFat, newdays)
- Carbmod <- predict(MCarb, newdays)
- Protmod <- predict(MProt, newdays)
- #Make plot
- png( filename = "Rplot.png", width = 666, height = 500, units = "px")
- par(mfrow = c(2, 3))
- plot(weid$Weight ~ weid$Days, xlab = "", ylab = "", ylim = c(90, 160),
- xlim = c(0, newt))
- par(new = T)
- plot(Weightmod$y ~ Weightmod$x, type = "l", xlab = "days", ylab = "body mass [kg]",
- ylim = c(90, 160), col = "red", xlim = c(0, newt))
- plot(weid$Body.Fat ~ weid$Days, xlab = "", ylab = "", ylim = c(10, 60),
- xlim = c(0, newt))
- par(new = T)
- plot(BFmod$y ~ BFmod$x, type = "l", xlab = "days", ylab = "body fat [%]",
- ylim = c(10, 60), col = "red", xlim = c(0, newt))
- plot(Fatmod$y ~ newdays, type = "l", xlab = "days", ylab = "calories from [%]",
- col = "red", ylim = c(0, 100), xlim = c(0, newt))
- par(new = T)
- plot(Carbmod$y ~ newdays, type = "l", xlab = "", ylab = "", col = "blue",
- ylim = c(0, 100), xlim = c(0, newt))
- par(new = T)
- plot(Protmod$y ~ newdays, type = "l", xlab = "", ylab = "", col = "darkgreen",
- ylim = c(0, 100), xlim = c(0, newt))
- text(20, 90, "Fat", col = "red", adj = c(0, 0))
- text(20, 80, "Carbohydrates", col = "blue", adj = c(0, 0))
- text(20, 70, "Protein", col = "darkgreen", adj = c(0, 0))
- plot(nutd$Calories ~ nutd$Days, xlab = "", ylab = "", ylim = c(1000, 4000),
- xlim = c(0, newt))
- par(new = T)
- plot(completecal ~ newdays, type = "l", xlab = "days", ylab = "calories consumed",
- ylim = c(1000, 4000), col = "red", xlim = c(0, newt))
- plot(completedef ~ newdays, type = "l", xlab = "days", ylab = "deficit to explain mass loss",
- ylim = c(-2000, 0), xlim = c(0, newt))
- plot(TDEE ~ newdays, type = "l", xlab = "days", ylab = "TDEE to explain mass loss",
- ylim = c(1000, 4000), xlim = c(0, newt))
- dev.off()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement