全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SPSS论坛
1438 3
2014-05-06
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
二维码

扫码加我 拉你入群

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

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

全部回复
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
二维码

扫码加我 拉你入群

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

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

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.
二维码

扫码加我 拉你入群

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

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

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
二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群