Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. # Loading data:
  2. load(url("https://github.com/pbiecek/Diagnoza/raw/master/data/gospodarstwa.rda"))
  3. load(url("https://github.com/pbiecek/Diagnoza/raw/master/data/osoby.rda"))
  4. load(url("https://github.com/pbiecek/Diagnoza/raw/master/data/gospodarstwaDict.rda"))
  5. load(url("https://github.com/pbiecek/Diagnoza/raw/master/data/osobyDict.rda"))
  6.  
  7. library("weights")
  8. library("Hmisc")
  9.  
  10.  
  11. #confidence interval of the satisfying household net income in 2003
  12. tmp<-gospodarstwa[,c('waga_gd_2003', 'bl6')]
  13. tmp<-na.omit(tmp)
  14.  
  15. weighted.ttest.ci <- function(x, weights, conf.level = 0.95) {
  16. require(Hmisc)
  17. nx <- length(x)
  18. df <- nx - 1
  19. vx <- wtd.var(x, weights, normwt = TRUE) ## From Hmisc
  20. mx <- weighted.mean(x, weights)
  21. stderr <- sqrt(vx/nx)
  22. tstat <- mx/stderr ## not mx - mu
  23. alpha <- 1 - conf.level
  24. cint <- qt(1 - alpha/2, df)
  25. cint <- tstat + c(-cint, cint)
  26. cint * stderr
  27. }
  28.  
  29. x<-t(tmp[2])
  30. weights<-t(tmp[1])
  31. wtd.hist(x=x, weight=weights, breaks=100, xlim=c(0,10000))
  32. answer <- weighted.ttest.ci(x,weights)
  33. #95% confidence interval is equal (3387,3544)
  34.  
  35. #----------------------------------------------------------------------------------------------
  36.  
  37. #hypothesis of having cars and bigger average net income
  38. tmp <- gospodarstwa[,c('fl2', 'ff9_12a', 'waga_gd_2011')]
  39. tmp1 <- subset(tmp, ff9_12a == 1)
  40. tmp2 <- subset(tmp, ff9_12a == 2)
  41.  
  42. wtd.t.test(x=tmp1$fl2, y=tmp2$fl2, weight=tmp1$waga_gd_2011, weighty = tmp2$waga_gd_2011, alternative = "more")
  43. #p value is basically 0, so it's below alpha=0.05 and we reject H0 hypothesis
  44. #car owners are gaining less than non-owners
  45. wtd.hist(x=tmp1$fl2, weight = tmp1$waga_gd_2011, breaks=100, xlim=c(0,10000))
  46. wtd.hist(x=tmp2$fl2, weight = tmp2$waga_gd_2011, breaks=100, xlim=c(0,6000))
  47. #comparing histograms we can see, that car owner's histogram is shifted to the right, with bigger mean 3927 > 1980
  48.  
  49.  
  50.  
  51. #----------------------------------------------------------------------------------------------
  52.  
  53. #hypothesis of phone owners being more happy than non-owners
  54.  
  55. tmp <- osoby[,c('ep3', 'ec25', 'waga_2009_osoby')]
  56. tmp <- na.omit(tmp)
  57. tmp1 <- subset(tmp, ec25 != 4)
  58. tmp2 <- subset(tmp, ec25 == 4)
  59.  
  60. wtd.t.test(x=tmp1$ep3, y=tmp2$ep3, weight=tmp1$waga_2009_osoby, weighty = tmp2$waga_osoby_2009, alternative = "more")
  61. wtd.hist(x=tmp1$ep3, weight=tmp1$waga_2009_osoby, breaks=6, xlim=c(0,7))
  62. wtd.hist(x=tmp2$ep3, weight=tmp2$waga_2009_osoby, breaks=6, xlim=c(0,7))
  63.  
  64. wtd.mean(x=tmp1$ep3, weight=tmp1$waga_2009_osoby)
  65.  
  66. wtd.mean(x=tmp2$ep3, weight=tmp2$waga_2009_osoby)
  67. #p value is basically 0, so it's below alpha=0.05 and we reject H0 hypothesis
  68. #phone owners are less happy than phone-free people
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement