state.R 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if(i>size_*2 && i<=((nrow(DATA) - size_))){
  39. tmp_ <- (
  40. tbl_df(
  41. table(
  42. POINTS[i-size_,] %>%
  43. unlist(., use.names=FALSE)
  44. )
  45. ) %>%
  46. arrange(
  47. desc(n)
  48. )
  49. )
  50. POINTS[i-size_,]$state <- tmp_[1,]$Var1
  51. POINTS[i-size_,]$probability <- ((1 / size_) * tmp_[1,]$n)
  52. }
  53. DATA[i,]$state = state_
  54. }
  55. print("STATE CALCULATION DONE")
  56. remove(tmp_)
  57. DATA <- DATA[complete.cases(DATA),]
  58. rownames(DATA) <- 1:nrow(DATA)
  59. DATA$index = as.integer(rownames(DATA))
  60. # USE PATIENT ENVIRONMENT
  61. source("code/patient.R")
  62. patient$WALKING <- strtoi(rownames(DATA[DATA$state==1,]))
  63. patient$SITTING <- group(strtoi(rownames(DATA[DATA$state==4,])), 10)
  64. patient$UP <- strtoi(rownames(DATA[DATA$state==2,]))
  65. patient$DOWN <- strtoi(rownames(DATA[DATA$state==3,]))
  66. patient$SITBASE <- mean(c(DATA[as.integer(unlist(patient$SITTING)),]$FootLeft.y, DATA[as.integer(unlist(patient$SITTING)),]$FootRight.y))
  67. if(!consistent(patient$WALKING)){
  68. stop("Patient not consistently walking, (Maybe he/she fell). Anyway, we can't analyse this data", call. = FALSE)
  69. }
  70. # CALCULATE STRAIGHT WALKING PATH
  71. yPrediction <-lm(Head.y ~ I(index^2)+index, data=DATA[min(patient$WALKING):max(patient$WALKING),])
  72. yPredicted <- as.vector(predict(yPrediction, data.frame(index=patient$WALKING)))
  73. patient$WALKBASE <- mean(c(yPredicted[1], tail(yPredicted, n=1)))
  74. for(i in patient$WALKING[1]:(length(patient$WALKING) + patient$WALKING[1] - 1)){
  75. DATA[i,]$Head.y <- DATA[i,]$Head.y - (yPredicted[i-patient$WALKING[1]+1] - patient$WALKBASE)
  76. }
  77. plot(POINTS$probability, type = "l")
  78. # http://stats.stackexchange.com/questions/30975/how-to-add-non-linear-trend-line-to-a-scatter-plot-in-r
  79. # http://www.mathsisfun.com/geometry/parabola.html