Advertisement
Guest User

calculate TDEE based on body mass and calories

a guest
Jul 14th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.91 KB | None | 0 0
  1. #Set input files
  2. nutfile = "nutrition.txt"
  3. weifile = "weight.txt"
  4.  
  5. #Ser smoothing parameters (from 0 to 2)
  6. smoothparam <- 1.1
  7. smoothparammacros <- 0.6
  8.  
  9. #Load libraries
  10. require("plyr")
  11.  
  12. #Read data files
  13. #Tab-delimited text files
  14. #
  15. #nutrition file
  16. #accepts multiple meals per day
  17. #
  18. #columns:
  19. #Date (dd/mm/yyyy)
  20. #Calories (number)
  21. #Fat (numeric, grams)
  22. #Carbohydrates (numeric, grams)
  23. #Protein (numeric, grams)
  24. #
  25. #weight file
  26. #accepts multiple measurements per day
  27. #
  28. #columns:
  29. #Date (dd/mm/yyyy)
  30. #Body.Fat.. (body fat %)
  31. #Weight (body mass, kilograms)
  32.  
  33. nut <- read.table(nutfile, header = T, sep = "\t")
  34. wei <- read.table(weifile, header = T, sep = "\t")
  35.  
  36. #Sum up all meals in each day
  37. nutd <- ddply(nut,
  38.               .(Date),
  39.               summarise,
  40.               Calories = sum(Calories),
  41.               Fat = sum(Fat),
  42.               Carbohydrates = sum(Carbohydrates),
  43.               Protein = sum(Protein),
  44.               PFat = 100 * (sum(Fat) * 9)/sum(Calories),
  45.               PCarbohydrates = 100 * (sum(Carbohydrates) * 4)/sum(Calories),
  46.               PProtein = 100 * (sum(Protein) * 4)/sum(Calories),
  47.               .progress = "text")
  48.  
  49. #Convert date to date format
  50. nutd$RDate <- as.Date(as.character(nutd$Date), "%d/%m/%Y")
  51. #order by date
  52. nutd <- nutd[order(nutd$RDate), ]
  53. #calculate number of days
  54. nutd$Days <- as.numeric(nutd$RDate - nutd$RDate[1])
  55. #remove rows with missing values
  56. nutd <- nutd[complete.cases(nutd), ]
  57.  
  58. #Average multiple weight/bf measurements in each day
  59. weid <- ddply(wei,
  60.               .(Date),
  61.               summarise,
  62.               Weight = mean(Weight),
  63.               Body.Fat = mean(Body.Fat..),
  64.               .progress = "text")
  65. #Convert date to date format
  66. weid$RDate <- as.Date(as.character(weid$Date), "%d/%m/%Y")
  67. #order by date
  68. weid <- weid[order(weid$RDate), ]
  69. #calculate number of days
  70. weid$Days <- as.numeric(weid$RDate - weid$RDate[1])
  71. #remove rows with missing values
  72. weid <- weid[complete.cases(weid), ]
  73.  
  74. #calculate the max number of days and create day sequence
  75. newt <- max(nutd$Days, weid$Days)
  76. newdays <- c(1:newt)
  77.  
  78. #fit smoothing spline to calories
  79. MCalories <- smooth.spline(nutd$Days, nutd$Calories, spar = smoothparam)
  80. Caloriesmod <- predict(MCalories, nutd$Days)
  81. nutd$Caloriesmod <- Caloriesmod$y
  82.  
  83. #fit smoothing spline to body mass and calculate deficit
  84. MWeight <- smooth.spline(weid$Days, weid$Weight, spar = smoothparam)
  85. Weightmod <- predict(MWeight, newdays)
  86. WeightmodD <- predict(MWeight, weid$Days, deriv = 1)
  87. #7700kcal per kilo of fat
  88. weid$def <- WeightmodD$y * 7700
  89.  
  90. #calculate TDEE for all days
  91. completecal <- predict(MCalories, newdays)$y
  92. completedef <- predict(MWeight, newdays, deriv = 1)$y * 7700
  93. TDEE <- completecal - completedef
  94.  
  95. #Fit body fat spline
  96. MBody.Fat <- smooth.spline(weid$Days, weid$Body.Fat, spar = smoothparam)
  97. BFmod <- predict(MBody.Fat, newdays)
  98. #Fit nutrient splines
  99. MFat <- smooth.spline(nutd$Days, nutd$PFat, spar = smoothparammacros)
  100. MCarb <- smooth.spline(nutd$Days, nutd$PCarbohydrates, spar = smoothparammacros)
  101. MProt <- smooth.spline(nutd$Days, nutd$PProtein, spar = smoothparammacros)
  102. Fatmod <- predict(MFat, newdays)
  103. Carbmod <- predict(MCarb, newdays)
  104. Protmod <- predict(MProt, newdays)
  105.  
  106. #Make plot
  107.  
  108. png( filename = "Rplot.png", width = 666, height = 500, units = "px")
  109. par(mfrow = c(2, 3))
  110.  
  111. plot(weid$Weight ~ weid$Days, xlab = "", ylab = "", ylim = c(90, 160),
  112.      xlim = c(0, newt))
  113. par(new = T)
  114. plot(Weightmod$y ~ Weightmod$x, type = "l", xlab = "days", ylab = "body mass [kg]",
  115.      ylim = c(90, 160), col = "red", xlim = c(0, newt))
  116. plot(weid$Body.Fat ~ weid$Days, xlab = "", ylab = "", ylim = c(10, 60),
  117.      xlim = c(0, newt))
  118. par(new = T)
  119. plot(BFmod$y ~ BFmod$x, type = "l", xlab = "days", ylab = "body fat [%]",
  120.      ylim = c(10, 60), col = "red", xlim = c(0, newt))
  121.  
  122. plot(Fatmod$y ~ newdays, type = "l", xlab = "days", ylab = "calories from [%]",
  123.      col = "red", ylim = c(0, 100), xlim = c(0, newt))
  124. par(new = T)
  125. plot(Carbmod$y ~ newdays, type = "l", xlab = "", ylab = "", col = "blue",
  126.      ylim = c(0, 100), xlim = c(0, newt))
  127. par(new = T)
  128. plot(Protmod$y ~ newdays, type = "l", xlab = "", ylab = "", col = "darkgreen",
  129.      ylim = c(0, 100), xlim = c(0, newt))
  130. text(20, 90, "Fat", col = "red", adj = c(0, 0))
  131. text(20, 80, "Carbohydrates", col = "blue", adj = c(0, 0))
  132. text(20, 70, "Protein", col = "darkgreen", adj = c(0, 0))
  133.  
  134. plot(nutd$Calories ~ nutd$Days, xlab = "", ylab = "", ylim = c(1000, 4000),
  135.      xlim = c(0, newt))
  136. par(new = T)
  137. plot(completecal ~ newdays, type = "l", xlab = "days", ylab = "calories consumed",
  138.      ylim = c(1000, 4000), col = "red", xlim = c(0, newt))
  139. plot(completedef ~ newdays, type = "l", xlab = "days", ylab = "deficit to explain mass loss",
  140.      ylim = c(-2000, 0), xlim = c(0, newt))
  141. plot(TDEE ~ newdays, type = "l", xlab = "days", ylab = "TDEE to explain mass loss",
  142.      ylim = c(1000, 4000), xlim = c(0, newt))
  143. dev.off()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement