Advertisement
Guest User

Untitled

a guest
May 26th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. id total_transfered_amount day
  2. 1 1000 2
  3. 1 2000 3
  4. 1 3000 4
  5. 2 1000 1
  6. 2 3000 4
  7. 2 5000 3
  8. 3 1000 4
  9. 3 2000 2
  10. 3 3000 3
  11. 4 1000 1
  12. 4 2000 2
  13. 4 3000 3
  14.  
  15. model_id_1=lm(day~total_transfered_amount)
  16. model_id_2=lm(day~total_transfered_amount)
  17. model_id_n=lm(day~total_transfered_amount)
  18.  
  19. library(dplyr)
  20. library(broom)
  21.  
  22. df %>% group_by(id) %>% do(tidy(lm(day~ total_transfered_amount, data=.)))
  23.  
  24. > df %>% group_by(id) %>% do(tidy(lm(day~ total_transfered_amount, data=.)))
  25. Source: local data frame [8 x 6]
  26. Groups: id [4]
  27.  
  28. id term estimate std.error statistic p.value
  29. (dbl) (chr) (dbl) (dbl) (dbl) (dbl)
  30. 1 1 (Intercept) 1.000000 0.0000000000 Inf 0.0000000
  31. 2 1 total_transfered_amount 0.001000 0.0000000000 Inf 0.0000000
  32. 3 2 (Intercept) 1.166667 1.9720265944 0.5916080 0.6599011
  33. 4 2 total_transfered_amount 0.000500 0.0005773503 0.8660254 0.5456289
  34. 5 3 (Intercept) 4.000000 1.8708286934 2.1380899 0.2785092
  35. 6 3 total_transfered_amount -0.000500 0.0008660254 -0.5773503 0.6666667
  36. 7 4 (Intercept) 0.000000 0.0000000000 NaN NaN
  37. 8 4 total_transfered_amount 0.001000 0.0000000000 Inf 0.0000000
  38.  
  39. df <- data.frame(id = c(1,1,1,2,2,2,3,3,3,4,4,4), total_transfered_amount = c(1000,2000,3000,1000,3000,5000,1000,2000,3000,1000,2000,3000), day=c(2,3,4,1,4,3,4,2,3,1,2,3))
  40. result <-df %>% group_by(id) %>% do (model = lm(.$day ~.$total_transfered_amount))
  41.  
  42. library(nlme)
  43. models_id <- lmList(day ~ total_transfered_amount| id, df)
  44.  
  45. models_id
  46. Call:
  47. Model: day ~ total_transfered_amount | id
  48. Data: df
  49.  
  50. Coefficients:
  51. (Intercept) total_transfered_amount
  52. 1 1.000000 1e-03
  53. 2 1.166667 5e-04
  54. 3 4.000000 -5e-04
  55. 4 0.000000 1e-03
  56.  
  57. Degrees of freedom: 12 total; 4 residual
  58. Residual standard error: 1.020621
  59.  
  60. list1 <- split(df, df$id)
  61. lapply(list1, function(i)lm(i$day ~ i$total_transfered_amount))
  62.  
  63. setDT(df)[, .(new = lm(day~total_transfered_amount)[1]), id]
  64. # id new
  65. #1: 1 1.000,0.001
  66. #2: 2 1.166667,0.000500
  67. #3: 3 4e+00,-5e-04
  68. #4: 4 0.000,0.001
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement