我有一个数据集,其中包括学生的所有要点和其他变量。
我还有一个对角线矩阵,其中包含有关哪个学生是另一个学生的同伴的信息。
现在我想用第二个矩阵(网络)来计算每个学生的平均同辈点数。 每个人都可以有不同数量的同龄人。 为了计算平均值,我将简单的0,1矩阵重新计算为百分比,其中分母是一个学生所拥有的同伴数量的总和。
第二个矩阵看起来像这样:
ID1 ID2 ID3 ID4 ID5 ID1 0 0 0 0 1 ID2 0 0 0.5 0.5 0 ID3 0 0.5 0 0 0.5 ID4 0 0.5 0 0 0.5 ID5 0.33 0 0.33 0.33 0每个学生的观点是另一个数据集中的一个简单变量,我希望将同位体平均点作为第二个变量:
ID Points Peers ID1 45 11 ID2 42 33.5 ID3 25 26.5 ID4 60 26.5 ID5 11 43.33Stata中是否有针对该问题的命令? 我目前正在研究Stata命令nwcommands ,但我不确定它是否可以提供帮助。 我可以使用Stata和R的解决方案。
I have one dataset which includes all the points of students and other variables.
I further have a diagonal matrix which includes information on which student is a peer of another student.
Now I would like to use the second matrix (network) to calculate the mean-peer-points for each student. Everyone can have different (number of) peers. To calculate the mean, I recalculated the simple 0,1 matrix into percentages, whereby the denominator is the sum of the number of peers one student has.
The second matrix then would look something like this:
ID1 ID2 ID3 ID4 ID5 ID1 0 0 0 0 1 ID2 0 0 0.5 0.5 0 ID3 0 0.5 0 0 0.5 ID4 0 0.5 0 0 0.5 ID5 0.33 0 0.33 0.33 0And the points of each students is a simple variable in another dataset, and I would like to have the peers-average-points in as a second variable:
ID Points Peers ID1 45 11 ID2 42 33.5 ID3 25 26.5 ID4 60 26.5 ID5 11 43.33Are there any commands in Stata for that problem? I am currently looking into the Stata commands nwcommands, but I am unsure whether it can help. I could use solutions for Stata and R.
最满意答案
nwcommands是一个我从未使用或研究过的优秀软件包,所以我会从第一个原则中尝试这个问题。 这是所有的矩阵代数,但给了一个矩阵和一个变量,我会在Stata中这样处理它。
clear scalar third = 1/3 mat M = (0,0,0,0,1\0,0,0.5,0.5,0\0,0.5,0,0,0.5\0,0.5,0,0,0.5\third,0,third,third,0) input ID Points Peers 1 45 11 2 42 33.5 3 25 26.5 4 60 26.5 5 11 43.33 end gen Wanted = 0 quietly forval i = 1/5 { forval j = 1/5 { replace Wanted = Wanted + M[`i', `j'] * Points[`j'] in `i' } } list +--------------------------------+ | ID Points Peers Wanted | |--------------------------------| 1. | 1 45 11 11 | 2. | 2 42 33.5 42.5 | 3. | 3 25 26.5 26.5 | 4. | 4 60 26.5 26.5 | 5. | 5 11 43.33 43.33334 | +--------------------------------+小点:对1/3使用0.33并不能提供足够的精度。 例如,1/6和1/7会产生类似的问题。
另外,我得到2的同龄人是3和4,所以他们的平均值是(25 + 60)/ 2 = 42.5,而不是33.5。
编辑:类似的方法开始于非常像@ ander2ed想象的数据结构
clear input int(id points id1 id2 id3 id4 id5) 1 45 0 0 0 0 1 2 42 0 0 1 1 0 3 25 0 1 0 0 1 4 60 0 1 0 0 1 5 11 1 0 1 1 0 end gen wanted = 0 quietly forval i = 1/5 { forval j = 1/5 { replace wanted = wanted + id`j'[`i'] * points[`j'] in `i' } } egen count = rowtotal(id1-id5) replace wanted = wanted/count list +--------------------------------------------------------------+ | id points id1 id2 id3 id4 id5 wanted count | |--------------------------------------------------------------| 1. | 1 45 0 0 0 0 1 11 1 | 2. | 2 42 0 0 1 1 0 42.5 2 | 3. | 3 25 0 1 0 0 1 26.5 2 | 4. | 4 60 0 1 0 0 1 26.5 2 | 5. | 5 11 1 0 1 1 0 43.33333 3 | +--------------------------------------------------------------+nwcommands is an outstanding package I have never used or studied, so I will just try the problem from first principles. This is all matrix algebra, but given a matrix and a variable, I would approach it like this in Stata.
clear scalar third = 1/3 mat M = (0,0,0,0,1\0,0,0.5,0.5,0\0,0.5,0,0,0.5\0,0.5,0,0,0.5\third,0,third,third,0) input ID Points Peers 1 45 11 2 42 33.5 3 25 26.5 4 60 26.5 5 11 43.33 end gen Wanted = 0 quietly forval i = 1/5 { forval j = 1/5 { replace Wanted = Wanted + M[`i', `j'] * Points[`j'] in `i' } } list +--------------------------------+ | ID Points Peers Wanted | |--------------------------------| 1. | 1 45 11 11 | 2. | 2 42 33.5 42.5 | 3. | 3 25 26.5 26.5 | 4. | 4 60 26.5 26.5 | 5. | 5 11 43.33 43.33334 | +--------------------------------+Small points: Using 0.33 for 1/3 doesn't give enough precision. You'll have similar problems for 1/6 and 1/7, for example.
Also, I get that the peers of 2 are 3 and 4 so their average is (25 + 60)/2 = 42.5, not 33.5.
EDIT: A similar approach starts with a data structure very like that imagined by @ander2ed
clear input int(id points id1 id2 id3 id4 id5) 1 45 0 0 0 0 1 2 42 0 0 1 1 0 3 25 0 1 0 0 1 4 60 0 1 0 0 1 5 11 1 0 1 1 0 end gen wanted = 0 quietly forval i = 1/5 { forval j = 1/5 { replace wanted = wanted + id`j'[`i'] * points[`j'] in `i' } } egen count = rowtotal(id1-id5) replace wanted = wanted/count list +--------------------------------------------------------------+ | id points id1 id2 id3 id4 id5 wanted count | |--------------------------------------------------------------| 1. | 1 45 0 0 0 0 1 11 1 | 2. | 2 42 0 0 1 1 0 42.5 2 | 3. | 3 25 0 1 0 0 1 26.5 2 | 4. | 4 60 0 1 0 0 1 26.5 2 | 5. | 5 11 1 0 1 1 0 43.33333 3 | +--------------------------------------------------------------+更多推荐
发布评论