全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 统计软件培训班VIP答疑区
1435 2
2009-10-30
  My teacher gave the R language as follows
p1=130
p2=113
set1=seq(2,402,10)
result=matrix(0,1,8)
for (n1 in set1){
rv1=matrix(0,100,1)
rv2=matrix(0,100,1)
for (j in 1:100)  {

x=mvrnorm(n=n1,rep(0,p1),diag(1,p1))
y=mvrnorm(n=n1,rep(0,p2),diag(1,p2))

num1=sum((t(y)%*%x)^2)
den1=sqrt(sum((x%*%t(x))^2)%*%sum((y%*%t(y))^2))
V1=(x%*%t(x))-diag(diag(x%*%t(x)))
W1=(y%*%t(y))-diag(diag(y%*%t(y)))
V2=matrix(V1,length(V1),1)
W2=matrix(W1,length(W1),1)
num2=t(V2)%*%W2
den2=sqrt((t(V2)%*%V2)%*%(t(W2)%*%W2))

rv1[j]=num1/den1
rv2[j]=num2/den2
}
rv3=(p1%*%p2)/sqrt((p1^2+(n1+1)%*%p1)*(p2^2+(n1+1)%*%p2))
result=rbind(result,cbind(n1,mean(rv1),min(rv1),max(rv1),rv3,mean(rv2),min(rv2),max(rv2)))
}
result=result[-1,]
result
cbind(seq(2,402,10),result[,5]-result[,2])
x1=result[,1]

plot(x1, result[,2], type='l', col="red")
points(x1, result[,3], type='l', col="red")
points(x1, result[,4], type='l', col="red")
plot(x1, result[,6], type='l',ylim=c(-1,1), xlim=c(0,500), col="red")
points(x1, result[,7], type='l',ylim=c(-1,1), xlim=c(0,500), col="red")
points(x1, result[,8], type='l',ylim=c(-1,1), xlim=c(0,500), col="red")
二维码

扫码加我 拉你入群

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

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

全部回复
2009-10-30 07:55:38
I can't understand  some places as follows:
1)for (n1 in set1)    and  for (j in 1:100)
2)x=mvrnorm(n=n1,rep(0,p1),diag(1,p1))
3)diag(diag(x%*%t(x))
4)rbind(result,cbind(n1,mean(rv1),min(rv1),max(rv1),rv3,mean(rv2),min(rv2),max(rv2)))
5)cbind(seq(2,402,10),result[,5]-result[,2])
二维码

扫码加我 拉你入群

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

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

2009-10-30 13:22:51
1)first ,set1=c(  2  12  22  32  42  52  62  72  82  92 102 112 122 132 142 152 162 172 182 192 202 212 222 232 242 252 262 272 282 292 302 312 322 332 342 352 362 372 382 392 402)
for (n1 in set1)  means that n1 pick one value from 2 to 402 in set1 every time ,this is a loop.
2)x=mvrnorm(n=n1,rep(0,p1),diag(1,p1))
   this command to generate n1 mulitvariate nomal distribution random variable. the mean are zero. the deviation are 1.
3) x%*%t(x) menas xx',diag(x%*%t(x)) means extract the diagonal values of matrix xx',
diag(diag(x%*%t(x)) means generate a unit matrix with the diagonal values of matrix xx'

4)rbind() means  combine vector by row,cbind()menas combine vector by col.

all the commands above ,you can run these in your Rconsole to make deeply understand.
二维码

扫码加我 拉你入群

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

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

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

说点什么

分享

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