R语言rugarch包内含的filter函数有何作用

Python09

R语言rugarch包内含的filter函数有何作用,第1张

本帖最后由 epoh 于 2013-2-3 14:13 编辑

这个问题有人问过,而alexios ghalanos 也回答了:

不会对这组新数据进行拟合

就是1-step ahead rolling forecast ,

你可以自行试试,的确是如此.

It does not "arrive at updated parameter estimates". The

ugarchfilter method simply "filters" the new dataset with the existing

estimated parameters to generate an "updated" conditional mean and

variance. It is exactly like performing a 1-step ahead rolling forecast

(since the filter generates t+1|t..)

R语言-v1-基础知识

Iretara  12-17 21:18

以例题的形式简述R语言基础知识

# 读取文件

setwd(" 文件链接的时候,用  /  ")

install.packages(" readxl ")

library(readxl)

library (tidyverse)

hw1_a<- read_excel ("hw1_a.xlsx", col_types=c("numeric", "numeric", "numeric", "numeric", "numeric") )

hw1_b<- read_excel ("hw1_b.xlsx")

#读取csv

library(readr)

hw1_a<- read_csv ("/")

View(hw1_a)

# 描述型函数

hw1_a + hw1_b 表

#描述最小值,最大值,中值,均值,标准差

Str (hw1_a) #查看数据并指出各个 变量的形式

summary (hw1_a) #指出各个变量的形式, 最小值,最大值,中值,均值

library(psych)

describe (hw1_a) #比summary更简便的方法, 可以直接读取标准差等;但是,使用describe不可读取 NA值, 可以尝试使用 Hmisc包中 describe

描述型函数-R

# 连接

hw1_a %>% inner_join (hw1_b, by ="ID")

hw1_a %>% left_join (hw1_b, by ="ID")

hw1_a %>% right_join (hw1_b, by ="ID")

hw1_a %>% full_join (hw1_b, by ="ID")

inner_join<- inner_join (hw1_a,hw1_b, by =“ID”) #报告合并后的 总行数 ,178行

full_join<- full_join (hw1_a,hw1_b, by ="ID")

( nrow (full_join)) #报告合并后的 总行数 ,200行

>  length (full_join$ID)

#找出各个列的 缺失值

i<-NA

a<-NA

for(i in 1:length(full_join[1,])){ a[i]<- sum(is.na( full_join[,i] ) ) }

paste("缺失值是",a)

#缺失值总数

sum(is.na(full_join))

#删除缺失值 na.omit()

full_join1=filter(full_join,!is.na(full_join[2]))

full_join1=filter(full_join1,!is.na(full_join1[3]))

full_join1=filter(full_join1,!is.na(full_join1[4]))

full_join1=filter(full_join1,!is.na(full_join1[5]))

full_join1=filter(full_join1,!is.na(full_join1[6]))

full_join1=filter(full_join1,!is.na(full_join1[7]))

full_join1=filter(full_join1,!is.na(full_join1[8]))

sum(is.na(full_join1))

找出Income中的 极端值 并滤掉对应行的数据

quantile (hw1_a$Income,c(0.025,0.975))

hw1_a2= filter (hw1_a,Income>14168.81 &Income<173030.92)

#使用dplyr进行数据转换

arrange()

>arrange (hw1_a,Income) #默认升序

>arrange(hw1_a, desc (Income)) #desc降序,NA排序一般最后

select()

>select (hw1_a, - (Years_at_Address:Income)) #不要变量

>rename (hw1_a, In_come=Income) #改名

>select(hw1_a,Income, exerything ()) #把Income放在前面

拓例题1:

library(nycflights13)

view(flights)

#counts

(1)

not_cancelled <- flights %>%

filter(! is.na(dep_delay), !is.na(arr_delay))

(2)

not_cancelled %>%

group_by (year,month,day) %>%

summarize (mean=mean(dep_delay))

(3)

delays <- not_cancelled %>%

group_by (tailnum) %>%

summarize (delay=mean(arr_delay))

ggplot (data=delays,mapping=aes(x= delay))+

geom_freqpoly (binwidth=10) #freqpoly

(4)

delays <- not_cancelled %>%

group_by(tailnum) %>%

summarize(delay=mean(arr_delay,na.rm=TRUE), n=n() ) #tailnum的次数

ggplot(data=delays,mapping=aes(x= n, y=delay))+

geom_point(alpha=1/10)

拓例题2:

#请按照价格的均值,产生新的变量price_new, 低于均值为“低价格”,高于均值为“高价格”。 同样对市场份额也是,产生变量marketshare_new, 数值为“低市场份额”和“高市场份额”

price=data1$price

pricebar=mean(price)

price_new= ifelse (price>pricebar,“高价格”,”低价格”)

marketshare=data1$marketshare

marketsharebar=mean(marketshare)

marketshare_new=ifelse(marketshare>marketsharebar ,“高市场份额”,”低市场份额”)

data1= mutate (data1,price_new,marketshare_new)

#可视化

#将Income 对数化

lninc<- log (hw1_a$Income)

#画出直方图和 density curve密度曲线

hist (lninc,prob=T)

lines ( density (lninc),col="blue")

# 添加额外变量 的办法,在 aes()中添加 样式 (color、size、alpha、shape)

ggplot(data=inner_join)+

geom_point(mapping = aes(x=Years_at_Employer,y= Income, alpha= Is_Default))

# 按照Is_Default 增加一个维度,使用明暗程度作为区分方式

ggplot(data=inner_join)+

geom_point(mapping = aes(x=Years_at_Employer,y= Income,

alpha=factor( Is_Default ) ))

#使用形状作为另外一种区分方式

ggplot(data=inner_join)+

geom_point(mapping = aes(x=Years_at_Employer,y= Income,

shape=factor( Is_Default)))

可视化-R

拓展:

#将 flight1 表和 weather1 表根据共同变量进行内连接,随机抽取 100000 行数据, 将生产的结果保存为 flight_weather。 (提示:sample_n()函数,不用重复抽取)

flight_weather <- inner_join(flight1, weather1) %>% sample_n(100000)

# 从 flight_weather表中对三个出发机场按照平均出发延误时间排降序,并将结果保留在 longest_delay表中。把结果展示出来

longest_delay<- flight_weather %>%

group_by(origin) %>%

summarize(delay=mean(dep_delay, na.rm=TRUE )) %>%

arrange(desc(delay))

#根据不同出发地(origin)在平行的 3 个图中画出风速 wind_speed(x 轴)和出发 延误时间 dep_delay(y 轴)的散点图。

ggplot(data= flight_weather) +

geom_point(mapping=aes(x=wind_speed,y=dep_delay))+

facet_grid(.~origin, nrow = 3 ) # 按照class分类,分成3行

#根据 flight_weather 表,画出每个月航班数的直方分布图,x 轴为月份,y 轴是每个 月份航班数所占的比例。

ggplot(data=flight_weather)+

geom_bar(mapping=aes(x=month, y=..prop .., group=1))

#根据 flight_weather 表,画出每个月航班距离的 boxplot 图,x 轴为月份,y 轴为 航行距离, 根据的航行距离的中位数从低到高对 x 轴的月份进行重新排序

ggplot(data=flight_weather)+

geom_boxplot(mapping=aes(x= reorder (month,distance,FUN=median),y=distance))

线性回归

# 以Income作为因变量,Years at Employer作为自变量,进行 OLS回归

m1<- lm (Income ~ Years_at_Employer,data=hw1_a)

#通过***判断显著性

summary (m1)

#画出拟合直线

ggplot(data= hw1_a)+

geom_point(aes(x=Income,y=Years_at_Employer))+

geom_abline(data= m1,col= "blue")

#证明拟合直线是最优的

b0=runif(20000,-5,5)

b1=runif(20000,-5,5)

d<-NA

sum<-NA

n<-1

while(n<=20000){

for(i in 1:24){

d[i]<-(hw1_a $ Income[i]-b0[n]-b1[n]*hw2$ Years_at_Employer[i])^2}

sum[n]<-sum(d)

n<-n+1

}

resi=m1$residuals

resi2=sum(resi^2)

check=sum(as.numeric(sum<resi2))

check