[英]Two R Error Messages When Attempting To Predict Via Single Index Model
I am attempting to use the Single Index model to estimate alpha beta and sigma2_ei for 3 stocks and the TSX. 我正在尝试使用单一指数模型来估计3只股票和TSX的alpha beta和sigma2_ei。 Here is my current R code 这是我当前的R代码
Importing the Data 导入数据
setwd("~/Desktop/R Data Sets") Ass2Data1 <- read.csv("~/Desktop/R Data Sets/Ass2DataSheet1.csv") View(Ass2Data1)
Transforming the data into matrix form 将数据转换为矩阵形式
b <- as.matrix(Ass2Data1)
Generating Initial Vectors and Matrices 生成初始向量和矩阵
x <- rep(0,60)
xx <- matrix(x, ncol=4, nrow=3)
stock <- rep(0,3)
alpha <- rep(0,3)
beta <- rep(0,3)
mse <- rep(0,3)
Rbar <- rep(0,3)
Ratio <- rep(0,3)
col1 <- rep(0,3)
col2 <- rep(0,3)
col3 <- rep(0,3)
col4 <- rep(0,3)
col5 <- rep(0,3)
Regressing each stock on the index and recording results 对指数中的每只股票进行回归并记录结果
for(i in 1:3){
alpha[i] <- lm(data=Ass2Data1,formula=Ass2Data1[,1] ~ Ass2Data1[,4]$coefficients[1])
beta[i] <- lm(data=Ass2Data1,formula=Ass2Data1[,2] ~ Ass2Data1[,4]$coefficients[2])
Rbar[i] <- alpha[i]+beta[i]*mean(b[,4])
mse[i] <-sum(lm(data=Ass2Data1,formula=Ass2Data1[,i] ~ Ass2Data1[,4])$residuals2)/(nrow(b)-2)
Ratio[i] <- (Rbar[i]/beta[i])
stock[i] <- i }
Creating table 建立表格
xx <- (cbind(stock,alpha,beta,Rbar,mse,Ratio))
However I keep getting the following error messages: 但是,我不断收到以下错误消息:
Error in Ass2Data1[, 4]$coefficients : $ operator is invalid for atomic vectors Ass2Data1 [,4] $ coefficients中的错误:$运算符对原子向量无效
and 和
Error in beta[i] * mean(b[, 4]) : non-numeric argument to binary operator beta [i] * mean(b [,4])中的错误:二进制运算符的非数字参数
If anybody could point me in the right direction with respect to what I'm doing wrong here it would be greatly appreciated. 如果有人可以针对我在这里做错的事情为我指明正确的方向,将不胜感激。
Check ?lm
, change the closing )
检查?lm
,更改结账)
alpha[i] <- lm(data=Ass2Data1,formula=Ass2Data1[,1] ~ Ass2Data1[,4])$coefficients[1]
beta[i] <- lm(data=Ass2Data1,formula=Ass2Data1[,2] ~ Ass2Data1[,4])$coefficients[2]
Better way is to get coefficients via coef
: 更好的方法是通过coef
获取coef
:
alpha[i] <- coef(lm(data=Ass2Data1,formula=Ass2Data1[,1] ~ Ass2Data1[,4]))[1]
beta[i] <- coef(lm(data=Ass2Data1,formula=Ass2Data1[,2] ~ Ass2Data1[,4]))[2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.