R_Language 板


LINE

[問題類型]: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) [軟體熟悉度]: 入門(寫過其他程式,只是對語法不熟悉) [問題敘述]: 弱大數法則我還勉強能用丟銅板來"實驗"給學生看... 但是CLT要怎麼做呢= = 所以想用R來做電腦輔助教學... 而且我想示範: 1.不是n>=30就叫做大樣本。 所以除了常態分配和長得比較對稱的均勻分配以外, 其他都選取很極端的機率分佈, 結果n=30之下作Jarque Bera test都拒絕虛無假設。 2.期望值和變異數存在且有限才可適用CLT。 所以我故意選了Cauchy分配來示範。 (抽自柯西分配母體的樣本其平均仍然是柯西分配) 不過,我最後用ks.test的結果怪怪的, 是不是參數沒設定好?(難道沒辦法讓他自己抓常態分配的參數嗎= =) (用SAS和SPSS都不用調參數....是不是用R作KS檢定一定要指定參數?) [程式範例]: #請記得先安裝TSA package library(TSA) #X~N(0,1) set.seed(1) par(mfrow=c(1,2)) popnorm<-rnorm(n=100000,mean=0, sd=1) mean(popnorm) var(popnorm) plot(density(popnorm)) meannorm<-array(0,dim=c(1000)) for (i in 1:1000) { meannorm[i]=mean(sample(popnorm,size=30,replace=TRUE)) } mean(meannorm) var(meannorm) plot(density(meannorm)) ks.test(meannorm,"pnorm") jarque.bera.test(meannorm) 結果: > ks.test(meannorm,"pnorm") One-sample Kolmogorov-Smirnov test data: meannorm D = 0.3406, p-value < 2.2e-16 alternative hypothesis: two-sided > jarque.bera.test(meannorm) Jarque Bera Test data: meannorm X-squared = 4.9045, df = 2, p-value = 0.0861 最後改用這個: mean<-mean(meannorm) sd<-sqrt(var(meannorm)) ks.test(meannorm,"pnorm",mean,sd) 結果: One-sample Kolmogorov-Smirnov test data: meannorm D = 0.029, p-value = 0.3692 alternative hypothesis: two-sided 沒有違背預期的結果。 [關鍵字]: 電腦輔助教學, 機率, 中央極限定理 附註:以下試驗其他機率分佈 http://ideone.com/UkhaJO 為節省版面,貼到網路上供參考。 --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.255.26.132
1F:→ clickhere:忘了 sqrt(30) ? 12/15 08:05
2F:→ clickhere:再把 CLT 看一遍. 12/15 08:07
3F:→ andrew43:ks.test()若要檢驗分配是要指定參數沒錯。 12/15 11:50
4F:→ andrew43:至於其它軟體不用指定參數,我猜是自動用樣本估的。 12/15 11:50
5F:→ clickhere:no. sqrt(n) 才會給你正確的 CLT. n=30是猜不到的. 12/15 12:02
mean<-mean(meannorm) sd<-sqrt(var(meannorm)) ↑這裡的SD=sqrt(樣本變異數)=sqrt(母體變異數/30)=母體標準差/sqrt(30) ks.test(meannorm,"pnorm",mean,sd) 結果: One-sample Kolmogorov-Smirnov test data: meannorm D = 0.029, p-value = 0.3692 alternative hypothesis: two-sided 這個部份的sd應該不用再除sqrt(30) 因為樣本的變異數已經是母體變異數除以30了。 > mean(popnorm) [1] -0.002244083 > var(popnorm) [1] 1.007059 > mean(meannorm) [1] 0.002210398 > var(meannorm) [1] 0.03560481 其中抽出樣本的變異數約為母體變異數/30 > var(popnorm)/30 [1] 0.03356863 所以使用CLT的時候,標準差不用再除以sqrt(30)囉... 主要的問題是,不曉得有沒有用KS檢定卻不需指定參數的方法? 類似SAS和SPSS的那種... 雖然用JB檢定是滿方便的,可是初等統計教科書大多沒寫這方法... 另外,抽樣的時候設定replace=TRUE應該沒問題吧? 我想說這樣比較能確保每個樣本都iid於母體分配。 --- 程式又大幅度改寫: #X~N(0,1) set.seed(1) par(mfrow=c(1,2)) popnorm<-rnorm(n=100000,mean=0, sd=1) meanpopnorm<-mean(popnorm) varpopnorm<-var(popnorm) print(paste("The mean of the population is",round(meanpopnorm,5))) print(paste("The variance of the population is", round(varpopnorm,5))) plot(density(popnorm)) cltnorm<-function(n){ meannorm<-array(0,dim=c(1000)) for (i in 1:1000) { meannorm[i]=mean(sample(popnorm,size=n,replace=TRUE)) } print("----------------------------------------------------------") print(paste("The mean of samples is",round(mean(meannorm),5))) print(paste(", and the variance of samples is",round(var(meannorm),5))) print(paste("According to CLT, the theoretical mean is", round(meanpopnorm,5))) print(paste(", and the theoretical variance of samples is",round(varpopnorm/n,5))) print("----------------------------------------------------------") plot(density(meannorm)) jarque.bera.test(meannorm) } 接下來重覆五次試驗(n=30): > cltnorm(30) [1] "----------------------------------------------------------" [1] "The mean of samples is 0.00355" [1] ", and the variance of samples is 0.03627" [1] "According to CLT, the theoretical mean is -0.00224" [1] ", and the theoretical variance of samples is 0.03357" [1] "----------------------------------------------------------" Jarque Bera Test data: meannorm X-squared = 4.4989, df = 2, p-value = 0.1055 > cltnorm(30) [1] "----------------------------------------------------------" [1] "The mean of samples is -0.00778" [1] ", and the variance of samples is 0.03286" [1] "According to CLT, the theoretical mean is -0.00224" [1] ", and the theoretical variance of samples is 0.03357" [1] "----------------------------------------------------------" Jarque Bera Test data: meannorm X-squared = 0.4475, df = 2, p-value = 0.7995 > > cltnorm(30) [1] "----------------------------------------------------------" [1] "The mean of samples is 0.00405" [1] ", and the variance of samples is 0.03348" [1] "According to CLT, the theoretical mean is -0.00224" [1] ", and the theoretical variance of samples is 0.03357" [1] "----------------------------------------------------------" Jarque Bera Test data: meannorm X-squared = 3.2785, df = 2, p-value = 0.1941 > cltnorm(30) [1] "----------------------------------------------------------" [1] "The mean of samples is -0.0048" [1] ", and the variance of samples is 0.03026" [1] "According to CLT, the theoretical mean is -0.00224" [1] ", and the theoretical variance of samples is 0.03357" [1] "----------------------------------------------------------" Jarque Bera Test data: meannorm X-squared = 3.2825, df = 2, p-value = 0.1937 > cltnorm(30) [1] "----------------------------------------------------------" [1] "The mean of samples is 5e-05" [1] ", and the variance of samples is 0.03214" [1] "According to CLT, the theoretical mean is -0.00224" [1] ", and the theoretical variance of samples is 0.03357" [1] "----------------------------------------------------------" Jarque Bera Test data: meannorm X-squared = 10.1117, df = 2, p-value = 0.006372 最後一次試驗錯誤的拒絕虛無假設... 我該怎麼解釋呢= =" --- 剛這樣寫: testing<-function(n, m){ set.seed(1) popnorm<-rnorm(n=100000,mean=0, sd=1) error<-array(0,dim=c(m)) for (i in 1:m){ meannorm<-array(0,dim=c(1000)) for (i in 1:1000) { meannorm[i]=mean(sample(popnorm,size=n,replace=TRUE)) } test<-jarque.bera.test(meannorm) print(test) p<-as.numeric(test$p.value) print(p) error[i]<-ifelse(p<0.05,1,0) print(error[i]) } return(list(matrix=error)) } 使用testing(30,50)測試, 可是傳回的矩陣很怪... 變成有1000個元素的一維陣列, 而且第五筆資料應該要存入1,卻反而是0。 ※ 編輯: anovachen 來自: 111.255.24.52 (12/15 23:48)
6F:→ andrew43:可再抽出沒錯,不過你有大母體了也沒什麼影響。 12/16 08:49
set.seed(1) popnorm<-rnorm(n=100000,mean=0, sd=1) error<-array(0,dim=c(30)) testing<-function(n){ meannorm<-array(0,dim=c(1000)) for (i in 1:1000) { meannorm[i]=mean(sample(popnorm,size=n,replace=TRUE)) } test<-jarque.bera.test(meannorm) print(test) p<-as.numeric(test$p.value) print(p) return(ifelse(p<0.05,1,0)) } for (i in 1:30){ error[i]<-testing(30) } 這樣寫就OK了。 不曉得上面寫法問題出在哪?? >"< ---- 以下是某不願具名者提供的程式碼: n <- 30 ret <- NULL for(i in 1:100){ x <- rnorm(n) x.hat <- mean(x) * sqrt(n) # CLT stat. goes to N(0, 1) in d. ret[i] <- x.hat } ks.test(ret, "pnorm") # Test 100 x.hat's against N(0, 1) 他指出我原本的寫法是類似bootstrap,在有限的樣本(N=100000)自助重抽樣, 以模擬從真正的母體裡抽樣的過程。 而他的寫法則比較貼近我想表達的。 依據他的寫法,我們可以如此改寫: n <- 100 ret <- NULL for(i in 1:100){ x <- runif(n,min=0,max=1) x.hat <- mean(x) ret[i] <- x.hat } hist(ret) ks.test(ret, "pnorm", mean=1/2, sd = sqrt(1/12/n)) 如此可驗證從U(0,1)中抽樣,n=100,則X-bar的分布 是否會漸近N(1/2, 1/1200)。 ※ 編輯: anovachen 來自: 111.255.13.49 (12/19 00:47)
7F:推 adgjlsfhk123:http://ccckmit.wikidot.com/st:centraltheorem 12/22 03:35
8F:→ adgjlsfhk123:希望對你有幫助 12/22 03:36
9F:→ anovachen:謝謝! 12/29 16:25







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Soft_Job站內搜尋

TOP