我有以下R函数。
Rsquared<-function(data1,data2){
output<-vector("numeric")
for (i in 1:25){
outcome<-lm(data1[,i]~Mkt.RF+SMB+HML,data=data2)
output<-c(output,print(summary(lm(data1[,i]~Mkt.RF+SMB+HML,data=data2))$r.squared))
}
matrix(output,ncol=5,dimnames=list(c("Small","2","3","4","Big"),c("Low","2","3","4","High")))
}
Rsquared(Returnmatr,HW1factors)
输出如下:
[1] 0.9128694
[1] 0.9355165
[1] 0.9504655
[1] 0.9458229
[1] 0.9468217
[1] 0.9459374
[1] 0.9438374
[1] 0.9312822
[1] 0.9434745
[1] 0.9507508
[1] 0.9478602
[1] 0.9141433
[1] 0.8994373
[1] 0.9018144
[1] 0.8917617
[1] 0.9337635
[1] 0.885965
[1] 0.8731716
[1] 0.8814383
[1] 0.8658548
[1] 0.9420449
[1] 0.9031595
[1] 0.8482048
[1] 0.8872736
[1] 0.8070716
Low 2 3 4 High
Small 0.9128694 0.9459374 0.9478602 0.9337635 0.9420449
2 0.9355165 0.9438374 0.9141433 0.8859650 0.9031595
3 0.9504655 0.9312822 0.8994373 0.8731716 0.8482048
4 0.9458229 0.9434745 0.9018144 0.8814383 0.8872736
Big 0.9468217 0.9507508 0.8917617 0.8658548 0.8070716
我的问题是: 我只想要矩阵作为输出,而不是上面的数字列表。我怎样才能避免这个列表?为什么我甚至得到这个列表,而不是只有矩阵作为输出?谢谢你的帮助!我有以下R函数
print
的结果是输出。 移除 print
应该可以解决你的问题。
Rsquared<-function(data1,data2){
output<-vector("numeric")
for (i in 1:25){
outcome<-lm(data1[,i]~Mkt.RF+SMB+HML,data=data2)
output<-c(output,summary(lm(data1[,i]~Mkt.RF+SMB+HML,data=data2))$r.squared)
}
matrix(output,ncol=5,dimnames=list(c("Small","2","3","4","Big"),c("Low","2","3","4","High")))
}
Rsquared(Returnmatr,HW1factors)