楼主: ReneeBK
1290 3

[问答] Sampling WITH replacemen​t used in bootstrapp​ing complex sampling [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49412 个
通用积分
51.9904
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
new file.
set seed 20130407.
input program.
    vector PopX(1000,f6.3).
    loop #i = 1 to 1000.
       compute PopX(#i) = rv.normal(0,1).
    end loop.
    end case.
    end file.
end input program.
execute.
dataset name madeup.
dataset activate madeup.

* this next command needs to be adapted to your data.
vector PopX = PopX1 to PopX1000.

* from pop of 1000 draw 100 samples of size 50 WITH replacement.
compute #PopSize = 1000.
compute #nSamples=100.
compute #nDraws=50.
numeric SampledX (f6.3) CasePicked (N7).
loop sample_id = 1 to #nSamples.
    loop draw = 1 to #nDraws.
       compute CasePicked = trunc(rv.uniform(1, (#PopSize+1))).
       compute SampledX = PopX(CasePicked).
       xsave outfile = 'c:\project\long1.sav' /keep =sample_id draw
CasePicked SampledX.
    end loop.
end loop.
execute.
get file= 'c:\project\long1.sav'.
dataset name longy.
split file by sample_id.
frequencies vars = casepicked /format=dfreq.
descriptives variables = SampledX.


Art Kendall
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Bootstrap Sampling Bootstra replace Complex complex

沙发
ReneeBK 发表于 2014-5-6 23:56:37 |只看作者 |坛友微信交流群
DEFINE SIM (!POS !TOKENS(1)).
MATRIX.
SAVE UNIFORM(!1,1) /OUTFILE * .
END MATRIX.
!ENDDEFINE .

DEFINE Boot (!POS !TOKENS(1) ).
SET MXLOOPS 1000000.
MATRIX.
GET Data / FILE * / VAR ALL.
COMPUTE SIndex=MAKE(NROW(DATA) *!1,3,0).
LOOP #=1 TO !1.
COMPUTE Offset=(#-1)*NROW(DATA) .
+  LOOP ##=1 TO 1000.
+    COMPUTE SIndex(Offset + ##,1)=#.
+    COMPUTE SIndex(Offset + ##,2)=##.
+  END LOOP.
END LOOP.
COMPUTE CaseInd=TRUNC(UNIFORM(NROW(DATA) *!1,1)*NROW(DATA)  + 1).
LOOP #=1 TO NROW(SIndex).
+  COMPUTE SIndex(#,3)=Data(CaseInd(#)).
END LOOP.
SAVE ({SIndex,CaseInd})/ OUTFILE * /VARIABLES Sample Index DataY OrigCase.
END MATRIX.
!ENDDEFINE .
NEW FILE.
** Call as follows ** .
* Simulates 1000 draws from Uniform *.
SIM 1000.

**Bootstrap 100 samples of Size (N: NROWS in data )from whatever active
file.
Boot 100.
----
SPLIT FILE BY Sample .
Blah Blah Blah....

David Marso

使用道具

藤椅
ReneeBK 发表于 2014-5-6 23:58:22 |只看作者 |坛友微信交流群
How much experience/expertise does it take to understand what is going on?    Most people would have to take it on faith.
Probably very machine efficient.  But I'm a curmudgeon about readability.

In the 60's the saying was "Machines are expensive, people are cheap."

Nowadays machines are cheap.

However, I do have to admit the I learn from the eloquence of your solutions.

Of course once SPSS lets one save to a dataset rather than a file none of of solutions will have to write to the disk.

it would be interesting to hear from list members how long it took them to understand my syntax and your syntax.

使用道具

板凳
ReneeBK 发表于 2014-5-7 00:00:19 |只看作者 |坛友微信交流群
Perhaps this simplified version will bust through some of the cranial blockage ;-) Experience/Expertise is earned through experience and sometimes hard knocks and faith is highly over-rated when it comes to 'programming'.

Caveat: I post my code for free.  I don't take responsibility for others use of it.  Whether they experience an epiphany, a craniogasm or relative frustration is not part of the equation.

--

DEFINE SIM (!POS !TOKENS(1)).
MATRIX.
SAVE UNIFORM(!1,1) /OUTFILE * .
END MATRIX.
!ENDDEFINE .


DEFINE Boot (!POS !TOKENS(1) /!POS !TOKENS(1)).

SET MXLOOPS 1000000.
MATRIX.
+  GET Data / FILE * / VAR ALL.
+  COMPUTE p=NROW(DATA).

+  COMPUTE nrepXp=!1*p.
+  COMPUTE SIndex=MAKE(nrepXp,1,0).
+  COMPUTE CaseInd=TRUNC(UNIFORM(nrepXp,1)* p  + 1).
+  LOOP #=1 TO nrepXp.
+    COMPUTE SIndex(#)=Data(CaseInd(#)).
+  END LOOP.
+  SAVE ({CaseInd,SIndex}) / OUTFILE * /VARIABLES OrigCase DataY .
END MATRIX.
COMPUTE samplenum=TRUNC(($CASENUM-1)/!2)+1 .
EXECUTE /*Optional */.

!ENDDEFINE .

NEW FILE.
SIM 1000.

Boot 100 1000.

David Marso

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-4 17:05