滞后于列/变量 SPSS
lag over columns/ variables SPSS
我想做一些我认为非常简单的事情。
我的(模拟)数据如下所示:
data list free/totalscore.1 to totalscore.5.
begin data.
1 2 6 7 10 1 4 9 11 12 0 2 4 6 9
end data.
这些是多次试验的总分(在此模拟数据中,从 1 到 5)。现在我想知道在每次试验中获得的分数。也就是说,我想从第n+1次试验中减去第n次试验中的值。
最简单的语法如下所示:
COMPUTE trialscore.1 = totalscore.2 - totalscore.1.
EXECUTE.
COMPUTE trialscore.2 = totalscore.3 - totalscore.2.
EXECUTE.
COMPUTE trialscore.3 = totalscore.4 - totalscore.3.
EXECUTE.
等等...
这样结果看起来像这样:
当然,对 200 多个变量执行此操作是不可能的,也不好玩。
我尝试使用 VECTOR 和 DO REPEAT 编写语法,如下所示:
COMPUTE #y = 1.
VECTOR totalscore = totalscore.1 to totalscore.5.
DO REPEAT trialscore = trialscore.1 to trialscore.5.
COMPUTE #y = #x + 1.
END REPEAT.
COMPUTE trialscore(#i) = totalscore(#y) - totalscore(#i).
EXECUTE.
但是没用。
任何帮助表示赞赏。
Ps。我已经研究过使用 LAG,但是它会遍历行,而我需要它一次遍历 1 列。
我假设 respid
是您的原始(唯一)记录标识符。
编辑:
如果您没有记录标识符,您可以很容易地创建一个虚拟标识符:
compute respid=$casenum.
exe.
编辑结束
您可以尝试重新构造数据,以便每个分数都是一个不同的记录:
varstocases
/make totalscore from totalscore.1 to totalscore.5
/index=scorenumber
/NULL=keep.
exe.
然后对您的案例进行排序,使分数按降序排列(以便打包使用 lag
函数):
sort cases by respid (a) scorenumber (d).
然后实际进行基于lag
的计算
do if respid=lag(respid).
compute trialscore=totalscore-lag(totalscore).
end if.
exe.
最后取消重组:
casestovars
/id=respid
/index=scorenumber.
exe.
您最终应该得到一组 totalscore
个变量(最后一个将为空),它将包含您需要的内容。
你可以这样使用do repeat
:
do repeat
before=totalscore.1 to totalscore.4
/after=totalscore.2 to totalscore.5
/diff=trialscore.1 to trialscore.4 .
compute diff=after-before.
end repeat.
我想做一些我认为非常简单的事情。 我的(模拟)数据如下所示:
data list free/totalscore.1 to totalscore.5.
begin data.
1 2 6 7 10 1 4 9 11 12 0 2 4 6 9
end data.
这些是多次试验的总分(在此模拟数据中,从 1 到 5)。现在我想知道在每次试验中获得的分数。也就是说,我想从第n+1次试验中减去第n次试验中的值。 最简单的语法如下所示:
COMPUTE trialscore.1 = totalscore.2 - totalscore.1.
EXECUTE.
COMPUTE trialscore.2 = totalscore.3 - totalscore.2.
EXECUTE.
COMPUTE trialscore.3 = totalscore.4 - totalscore.3.
EXECUTE.
等等... 这样结果看起来像这样:
当然,对 200 多个变量执行此操作是不可能的,也不好玩。 我尝试使用 VECTOR 和 DO REPEAT 编写语法,如下所示:
COMPUTE #y = 1.
VECTOR totalscore = totalscore.1 to totalscore.5.
DO REPEAT trialscore = trialscore.1 to trialscore.5.
COMPUTE #y = #x + 1.
END REPEAT.
COMPUTE trialscore(#i) = totalscore(#y) - totalscore(#i).
EXECUTE.
但是没用。 任何帮助表示赞赏。
Ps。我已经研究过使用 LAG,但是它会遍历行,而我需要它一次遍历 1 列。
我假设 respid
是您的原始(唯一)记录标识符。
编辑:
如果您没有记录标识符,您可以很容易地创建一个虚拟标识符:
compute respid=$casenum.
exe.
编辑结束
您可以尝试重新构造数据,以便每个分数都是一个不同的记录:
varstocases
/make totalscore from totalscore.1 to totalscore.5
/index=scorenumber
/NULL=keep.
exe.
然后对您的案例进行排序,使分数按降序排列(以便打包使用 lag
函数):
sort cases by respid (a) scorenumber (d).
然后实际进行基于lag
的计算
do if respid=lag(respid).
compute trialscore=totalscore-lag(totalscore).
end if.
exe.
最后取消重组:
casestovars
/id=respid
/index=scorenumber.
exe.
您最终应该得到一组 totalscore
个变量(最后一个将为空),它将包含您需要的内容。
你可以这样使用do repeat
:
do repeat
before=totalscore.1 to totalscore.4
/after=totalscore.2 to totalscore.5
/diff=trialscore.1 to trialscore.4 .
compute diff=after-before.
end repeat.