iT邦幫忙

0

大家好:
小弟近日整理股票資料時遇到以下問題,想以每支股票各自的平均報酬率跟市場報酬率做簡單線性迴歸(平均報酬率~市場報酬率),計算出它的殘差,因為有910支股票,卡在不知道要怎麼做910次/images/emoticon/emoticon06.gif
資料樣貌:
https://ithelp.ithome.com.tw/upload/images/20200622/20116986FcmAtGkHO9.png
檔案路徑:
https://drive.google.com/drive/u/1/folders/1_h0tPiYruk59xZSG5Dg5mxW7JskwJV4z

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2020-06-22 16:43:03

參考這篇寫的

# 使用絕對路徑
#my.stock.df <- read.csv("C:\\it.csv")

my.stock.data.frame <- data.frame(
  period = c("2008/1/2","2008/1/3","2008/1/4","2008/1/7","2008/1/8","2008/1/9","2008/1/10"),
  stockno = c("1","1","1","1","1","1","1"),  
  marketrate = rnorm(7),
  averagerate = rnorm(7)
)

stock.lm <- lm(averagerate ~ marketrate, data = my.stock.data.frame)

summary(stock.lm)

# 常態性檢定
shapiro.test(stock.lm$residual)

# 殘差獨立性檢定
require(car)
durbinWatsonTest(stock.lm)

# 殘差變異數同質性檢定
require(car)
ncvTest(stock.lm)

感謝大大的指導,這是我目前的coding

library(dplyr)
library(tidyr)
library(data.table)
library(reshape2)
#剔除6642 大盤無
data1 <- fread(file="D:\\it",header=TRUE)
data1$市場報酬[is.na(data1$市場報酬)] <- 0
data1$return[is.na(data1$return)] <- 0
fm <-lm(市場報酬~return,data1)
residual <- as.data.frame(fm$residual)
lm_final <- cbind(data1,residual)
colnames(lm_final) <- c("週期", "股票編號","市場報酬率","平均報酬率","殘差")

然後目前是對所有股票做迴歸取殘差,但想要對每個股票(依照股票編號分類)各自去做迴歸,上面沒說清楚拍謝/images/emoticon/emoticon06.gif

沒關係
因為我不會
/images/emoticon/emoticon25.gif

還是很謝謝大大的資訊,我繼續找答案/images/emoticon/emoticon41.gif

我要發表回答

立即登入回答