2
0

state.R 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # CALCULATE PATIENT STATE ALGORITHM
  2. STATE = c("WALK", "UP", "DOWN", "SIT")
  3. size_ <- 5
  4. differ_ <- 0.1
  5. POINTS <- data.frame(
  6. index = integer(1)
  7. )
  8. POINTS["state"] <- 0
  9. POINTS["probability"] <- 0
  10. for(i in 1:size_){
  11. POINTS[LETTERS[i]] <- 0
  12. }
  13. for(i in size_:(nrow(DATA)-size_)){
  14. start <- DATA[i,]$Head.y
  15. end <- DATA[i+size_,]$Head.y
  16. state_ <- NULL
  17. if(((start - end) < differ_) && (start - end) > -differ_){
  18. if(start >= mean(DATA$Head.y)){ #WALK (4)
  19. state_ <- 1
  20. }
  21. else{ #SIT (4)
  22. state_ <- 4
  23. }
  24. }
  25. else if((start - end) < differ_) { #UP (2)
  26. state_ <- 2
  27. }
  28. else if((start - end) > differ_) { #UP (3)
  29. state_ <- 3
  30. }
  31. else {
  32. state_ <- 1
  33. }
  34. for(j in 0:size_-1){
  35. POINTS[i-j,LETTERS[j+1]] <- state_
  36. }
  37. POINTS[i,]$index <- DATA[i,]$Time
  38. remove(j)
  39. if(i>size_*2 && i<=((nrow(DATA) - size_))){
  40. tmp_ <- (
  41. tbl_df(
  42. table(
  43. POINTS[i-size_,] %>%
  44. unlist(., use.names=FALSE)
  45. )
  46. ) %>%
  47. arrange(
  48. desc(n)
  49. )
  50. )
  51. POINTS[i-size_,]$state <- tmp_[1,]$Var1
  52. POINTS[i-size_,]$probability <- ((1 / size_) * tmp_[1,]$n)
  53. }
  54. DATA[i,]$state = state_
  55. }
  56. remove(tmp_)
  57. remove(state_)
  58. remove(i)
  59. remove(start)
  60. remove(end)
  61. DATA <- DATA[complete.cases(DATA),]
  62. rownames(DATA) <- 1:nrow(DATA)
  63. DATA$index = as.integer(rownames(DATA))
  64. print("STATE CALCULATION DONE")
  65. # USE PATIENT ENVIRONMENT
  66. source("code/patient.R")
  67. patient$WALKING <- group(strtoi(rownames(DATA[DATA$state==1,])), 10)
  68. patient$SITTING <- group(strtoi(rownames(DATA[DATA$state==4,])), 10)
  69. patient$UP <- group(strtoi(rownames(DATA[DATA$state==2,])), 5)
  70. patient$DOWN <- group(strtoi(rownames(DATA[DATA$state==3,])), 5)
  71. patient$SITBASE <- mean(c(DATA[as.integer(unlist(patient$SITTING)),]$FootLeft.y, DATA[as.integer(unlist(patient$SITTING)),]$FootRight.y))
  72. if(!consistent(as.integer(unlist(patient$WALKING)))){
  73. stop("Patient not consistently walking, (Maybe he/she fell). Anyway, we can't analyse this data", call. = FALSE)
  74. }
  75. patient$WALKING <- as.integer(unlist(patient$WALKING))
  76. # CALCULATE STRAIGHT WALKING PATH
  77. yPrediction <-lm(Head.y ~ I(index^2)+index, data=DATA[min(patient$WALKING):max(patient$WALKING),])
  78. yPredicted <- as.vector(predict(yPrediction, data.frame(index=patient$WALKING)))
  79. patient$WALKBASE <- mean(c(yPredicted[1], tail(yPredicted, n=1)))
  80. patient$WALKERROR <- yPredicted - patient$WALKBASE
  81. for(i in patient$WALKING[1]:(length(patient$WALKING) + patient$WALKING[1] - 1)){
  82. #DATA[i,]$Head.y <- DATA[i,]$Head.y - (yPredicted[i-patient$WALKING[1]+1] - patient$WALKBASE)
  83. #DATA[,grep(".y", colnames(DATA))]
  84. for(j in colnames(DATA[i,grep(".y", colnames(DATA))])){
  85. DATA[i,j] <- DATA[i,j] - patient$WALKERROR[i - patient$WALKING[1] + 1]
  86. }
  87. }
  88. remove(yPrediction)
  89. remove(yPredicted)
  90. plot(POINTS$probability, type = "l")
  91. print("FILLING PATIENT CLASS DONE")
  92. # http://stats.stackexchange.com/questions/30975/how-to-add-non-linear-trend-line-to-a-scatter-plot-in-r
  93. # http://www.mathsisfun.com/geometry/parabola.html