全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SPSS论坛
1989 15
2014-05-07
Dear people on the list,
I have tried to get my head around one syntax command. My problems are:

  • I have an empirical distribution of a variable x with, say, 1000 observations.
  • I want to take 100 (or n) amount of random samples (with replacement) from x so that each sample size is, for example, 10 % of the x.
  • I need those random samples as variables x1...x100 into a new data set.
  • Is there a possibility to plot several histograms (of different variables) as a matrix with set dimensions (say 3x3 matrix)? This is a common way to plot your results, but I haven't yet figured out any other way than reordering the whole data into a list with a grouping variable of the old variables and then use that "variable group" as categorical variable in a panel plot for the whole list...


Thanks a lot!

Petro P.

二维码

扫码加我 拉你入群

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

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

全部回复
2014-5-7 00:05:09
Yes, I don't know of an easy way to random sample. Below is a thought I had awhile ago of generating a second dataset and then table matching to that. Unfortunately this approach can't be written into a MACRO (you can't include INPUT PROGRAM in a MACRO) - so I look forward to other solutions. This just takes advantage of the random uniform sampling procedure, between
1 and n of the original sample size, and then makes 9 runs. The data isn't returned in wide format like requested, but IMO it is frequently better to have the data like this anyway and use the SPLIT procedure to return stats on the subsets.

************************************************************************************************.
*Original Dataset.
set seed = 5.
input program.
loop #i = 1 to 1600.
     compute X = RV.NORMAL(0,1).
     compute id = #i.
     end case.
end loop.
end file.
end input program.
dataset name orig.
exe.


*Making a dataset with random samples with replacement - need to know N of
original dataset beforehand.
set seed = 10.
input program.
loop #iter = 1 to 9.             /*This is the number of replications */.
     loop #rand = 1 to 100.    /*This is the number of random samples with
replacement */.
         compute #n = 1600.   /*You need to supply this info - this is the
number of records in original database */.
         compute id = TRUNC(RV.UNIFORM(1,#n + 1)).
         compute run = #iter.
         end case.
     end loop.
end loop.
end file.
end input program.
dataset name rand_samps.
exe.

*now just table match the orig dataset to the random samples dataset.
dataset activate rand_samps.
sort cases by id.
match files file = *
/table = 'orig'
/by id.
exe.
************************************************************************************************.

I wonder if the newer bootstrapping procedures (or maybe even the old one in NLR) can be hacked to return the needed ID's with replacement. A matrix procedure should be possible as well (which can be called in a macro), but I'm not as saavy with that to give a quick answer. I don't know what your asking for 4, SPSS can produce small multiples if that is what your asking.
Andy W

二维码

扫码加我 拉你入群

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

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

2014-5-7 00:07:31
"you can't include INPUT PROGRAM in a MACRO) - so I look forward to other solutions. "
Sure you can.
Err... Have you tried?
INPUT PROGRAM is perfectly usable within MACRO!!
BEGIN DATA and END DATA are not permitted (for some reason which maybe Jon Peck can elaborate).
二维码

扫码加我 拉你入群

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

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

2014-5-7 00:08:34
Perhaps I have missed it but has anyone suggested the old SPSS command "Sample"?  Here is info from the SPSS v18
syntax reference:

SAMPLE

SAMPLE {decimal value} or {n FROM m }

This command does not read the active dataset. It is stored, pending execution with the next command that reads the dataset. For more information, see the topic Command Order on p. 40.
Example
SAMPLE .25.
Overview

SAMPLE permanently draws a random sample of cases for processing in all subsequent procedures.

For a temporary sample, use a TEMPORARY command before SAMPLE.

Basic Specification

The basic specification is either a decimal value between 0 and 1 or the sample size followed by keyword FROM and the size of the active dataset.
  • To select an approximate percentage of cases, specify a decimal value between 0 and 1.
  • To select an exact-size random sample, specify a positive integer that is less than the file size, and follow it with keyword FROM and the file size.

Operations
  • SAMPLE is a permanent transformation.
  • Sampling is based on a pseudo-random-number generator that depends on a seed value that is established by the program. On some implementations of the program, this number defaults to a fixed integer, and a SAMPLE command that specifies n FROM m will generate the identical sample whenever a session is rerun. To generate a different sample each time, use the SET command to reset SEED to a different value for each session. See the SET command for more information.

So, I think something like:

Temporary.
Sample 100 from 1000.
save outfile= etc.

Probably embed it in a loop or other structure to generate as many samples as one wants (probably create a new variable
ranging from 1 to 100 in all files which would allow one to use Match files to combine them all into a single file). Or something like that.

Mike Palij
二维码

扫码加我 拉你入群

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

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

2014-5-7 00:09:26
提示: 作者被禁止或删除 内容自动屏蔽
二维码

扫码加我 拉你入群

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

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

2014-5-7 00:14:08
new file.
input program.
    loop id = 1 to 1000.
       compute x = rv.normal(0,1).

       end case.
    end loop.
    end file.
end input program.

formats x(f6.3).
execute.
dataset name madeup.


new file.
set seed 20130307.
input program.
    vector sampleflag (100,f1).
    loop id = 1 to 1000.
       loop #sample= 1 to 100.
          compute sampleflag(#sample) =rv.uniform(0,1) le .10.
       end loop.

       end case.
    end loop.
    end file.
end input program.

dataset name sampleflags.
descriptives vars= sampleflag1 to sampleflag100.

match file file= madeup/file=sampleflags /by id.
dataset name combined.
do repeat
    xsample = xsample1 to xsample100
    /flag = sampleflag1 to sampleflag100.
    do if not sampleflag.
       compute xsample = 99999.
    else if sampleflag.
       compute xsample = x.
    ELSE.
       print 'oops!'.
    end if.
end repeat.
formats xsample1 to xsample100 (f6.3).
missing values xsample1 to xsample100 (99999).
descriptives vars = xsample1 to xsample100.

Art Kendall
二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

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