Você está na página 1de 4

7 * (3 + 2)/2

2^3
sqrt(9)
log(10) # logaritmo neperiano
exp(log(10))
x <- 5
a <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
a
a1 <- a/9
a1
a2 <- a^2
a2
a3 <- sqrt(a)
sum(a)
mean.a <- mean(a)
mean.a
help.start()
help(log)
?log
ls()
rm(a)
rm(list=ls())
ls()
# incluir a novamente
install.packages("fGarch")
m1 <- matrix(c(1,2,4,3,2,5,4,3,6,1,0,1,5,3,2,1),nrow=4)
m1
m2 <- matrix(c(1,2,4,3,2,5,4,3,6,1,0,1,5,3,2,1),nrow=8)
m2
det(m1)
m1.inv <- solve(m1)
m1.inv
det(m1.inv)
1/det(m1)
Y <- matrix(c(1,2,3,4), nrow=4)
Y
beta <- solve(m1)%*%Y
beta
a.cum <- cumsum(a)
a.cum
m3 <- apply(m1,2,cumsum)
m3
m4 <- apply(m1,2,cumprod)
m4
m5 <- array(1,c(4,4))
m5
m6 <- diag(4)
m6
z<-matrix(c(1,2,3,4,5,6), nrow=3)
z
mean.f.row <- apply(z,1,mean)
mean.f.row
mean.f.col <- apply(z,2,mean)
mean.f.col
# function
g <- function(x1,x2,x3){ exp(x1)*cos(x2*pi)*sqrt(x3) }
g(0,1,1)
f1 <- function(x) {1 + x}

f2 <- function(x) { 1 + x + x^2/2}


f3 <- function(x) {1 + x + x^2/2 + x^3/6}
x <- seq(from=-1, to=1, by=0.01)
y1 <- f1(x)
y2 <- f2(x)
y3 <- f3(x)
y4 <- exp(x)
plot(x,y1,type="l",col="red", ylab=expression(paste(g(x))),
main="aproximao da funo exponencial por Taylor")
lines(x,y2, type="l", col="green")
lines(x,y3,type="l", col="magenta")
lines(x,y4, type="l", col="blue", lwd=1, lty=2)
legend("topleft", col=c("red","green","magenta","blue"), lty=c(1,1,1,2),
legend=c("linear","parbola", "cbia", "exp"))
# nmeros aleatrios de uma N(0,1)
par(mfrow=c(1,1))
set.seed(877)
y <- rnorm(1000)
hist(y,30)
# linear regression
library(Ecdat)
data(Capm)
attach(Capm)
head(Capm)
length(rfood)
rfood <- rfood/100
rmrf <- rmrf/100
par(mfrow=c(1,1))
plot(rmrf,rfood,ylab="Food industry excess return",
xlab="Market excess return")
fit <- lm(rfood~rmrf)
abline(fit, col="red")
summary(fit)
anova(fit)
vcov(fit)
coefficients(fit)
confint(fit, level=0.95)
residuals(fit)
str(fit)
fit$coefficients
hist(fit$residuals)
# importando dados
library(fImport)
Y <- yahooSeries("PBR", from="2005-01-01", to="2016-05-10")
head(Y)
price <- Y[,"PBR.Close"]
library(quantmod)
chartSeries(price, theme="white")
chartSeries(price, chartTheme('white'))
library(timeSeries)
ret <- returns(price)
length(price)
length(ret)
par(mfrow=c(2,1))

plot(price, type="l", ylab="price", xlab="data", main="Petrobras")


plot(ret,type ="l", ylab="return", xlab="data")
par(mfrow=c(1,1))
hist(ret, n=50, xlim=c(-0.7,0.3))
library(fGarch)
par(mfrow=c(1,1))
qqnorm(ret); qqline(ret,col=2)
x<-seq(-.15,.15,by=0.001)
hist(ret,50, prob=TRUE, ylim=c(0,30),xlim=c(-0.15,0.15))
lines(density(ret, adjust=1), lwd=2, ylim=c(0,60), xlim=c(-0.05,0.05))
lines(x,dstd(x, mean=mean(ret), sd=sd(ret), 4), lty=5, lwd=2, col="red")
lines(x,dnorm(x,mean=mean(ret),sd=sd(ret)), lty=3, lwd=4, col="blue")
legend("topleft",c("KDE","t: df=5", "normal"), lwd=c(2,2,4), lty=c(1,5,3), col=c
("black","red","blue"))
# random walk
set.seed(123456)
mu <- 0.99
e <- rnorm(500)
## pure random walk
rw.nd <- cumsum(e)
## trend
trd <- 1:500
## random walk with drift
rw.wd <- mu*trd + cumsum(e)
## deterministic trend and noise
dt <- e + mu*trd
## plotting
par(mar=rep(5,4))
plot.ts(dt, lty=1, ylab='', xlab='')
lines(rw.wd, lty=2)
par(new=T)
# plot on the right axis
plot.ts(rw.nd, lty=3, axes=FALSE)
axis(4, pretty(range(rw.nd)))
lines(rw.nd, lty=3)
legend(10, 18.7, legend=c('det. trend + noise (ls)',
'rw drift (ls)', 'rw (rs)'),
lty=c(1, 2, 3))
# or can use the plot code ....
plot(dt,type="l", lty=1, ylab="", xlab="", col="blue")
lines(rw.wd, lty=2, col="red")
par(new=T)
plot(rw.nd,type="l", lty=3, axes=FALSE, ylab="",
xlab="time", col="green")
axis(4)
legend(40, 18.7, legend=c('det. trend + noise','random walk+drift',
'random walk'),lty=c(1, 2, 3),
col=c("blue","red","green"))
# plot 3D
library(scatterplot3d)
set.seed(124)
x <- rnorm(1000); y <- rnorm(1000)

z <- x^2 + y^2


par(mfrow = c(1,1), pty='s')
scatterplot3d(x,y,z,highlight.3d = T, col.axis = 1, col.grid = 1, pch=20)

Você também pode gostar