Diferenças
Aqui você vê as diferenças entre duas revisões dessa página.
Próxima revisão | Revisão anterior | ||
cursos:ruel:sessao0 [2007/12/18 17:50] – criada paulojus | cursos:ruel:sessao0 [2008/10/28 11:39] (atual) – paulojus | ||
---|---|---|---|
Linha 1: | Linha 1: | ||
====== Sessão Inicial -- Fundamentos da Linguagem R ====== | ====== Sessão Inicial -- Fundamentos da Linguagem R ====== | ||
+ | |||
+ | Criando um diretório (pasta) de trabalho, e mudando o // | ||
+ | <code R> | ||
+ | getwd() | ||
+ | dir.create(" | ||
+ | setwd(" | ||
+ | getwd() | ||
+ | </ | ||
+ | |||
+ | Vamos usar o conjunto de dados '' | ||
<code R> | <code R> | ||
require(MASS) | require(MASS) | ||
Linha 7: | Linha 17: | ||
rownames(hills) | rownames(hills) | ||
class(hills) | class(hills) | ||
- | #mh <- edit(hills) | + | </code> |
+ | Duplicando e modificando o conjunto de dados em outro objeto | ||
+ | <code R> | ||
+ | mh <- edit(hills) | ||
+ | </ | ||
+ | |||
+ | <code R> | ||
str(hills) | str(hills) | ||
Linha 23: | Linha 39: | ||
y <- sample(c(" | y <- sample(c(" | ||
y | y | ||
+ | ################################# | ||
mean(x[y == " | mean(x[y == " | ||
mean(x[y == " | mean(x[y == " | ||
Linha 96: | Linha 112: | ||
str(ml) | str(ml) | ||
length(ml) | length(ml) | ||
+ | |||
+ | |||
+ | ml$b | ||
+ | |||
+ | is.list(ml) | ||
+ | is.list(ml$b) | ||
+ | is.matrix(ml$b) | ||
+ | is.data.frame(ml$b) | ||
+ | |||
+ | fc <- function(x){ | ||
+ | 100* sd(x)/ | ||
+ | } | ||
+ | |||
+ | fc <- function(x){ | ||
+ | if(!is.numeric(x)) | ||
+ | stop(" | ||
+ | 100* sd(x)/ | ||
+ | } | ||
+ | fc(pessoas$peso) | ||
+ | fc(pessoas$sexo) | ||
+ | |||
+ | fc1 <- function(x){ | ||
+ | if(is.numeric(x)) res <- 100* sd(x)/ | ||
+ | else res <- table(x) | ||
+ | return(res) | ||
+ | } | ||
+ | |||
+ | fc1(pessoas$peso) | ||
+ | fc1(pessoas$sexo) | ||
+ | |||
+ | fc2 <- function(x) { | ||
+ | if(is.numeric(x)) { | ||
+ | m <- mean(x) | ||
+ | s <- sd(x) | ||
+ | cv <- 100*s/m | ||
+ | ai <- diff(range(x)) | ||
+ | res <- c(média=m, " | ||
+ | } | ||
+ | else{ | ||
+ | tb <- table(x) | ||
+ | moda <- tb[which.max(tb)] | ||
+ | res <- list(frequencias=tb, | ||
+ | } | ||
+ | return(res) | ||
+ | } | ||
+ | |||
+ | fc2(pessoas$peso) | ||
+ | fc2(pessoas$sexo) | ||
+ | |||
+ | lapply(pessoas, | ||
+ | |||
+ | df.res <- lapply(pessoas, | ||
+ | df.res | ||
+ | is.list(df.res) | ||
+ | str(df.res) | ||
+ | |||
+ | summary(pessoas) | ||
+ | sum.res <- summary(pessoas) | ||
+ | sum.res | ||
+ | class(sum.res) | ||
+ | str(sum.res) | ||
+ | |||
+ | ## redirecionando saida para um arquivo texto | ||
+ | sink(" | ||
+ | summary(pessoas) | ||
+ | lapply(pessoas, | ||
+ | pessoas | ||
+ | sink() ## voltando para o dispositivo de saida padrão (tela) | ||
+ | |||
+ | summary(pessoas) | ||
+ | |||
+ | |||
+ | head(hills) | ||
+ | |||
+ | ## 3 formas de fazxer o gráfico | ||
+ | plot(hills$dist, | ||
+ | with(hills, plot(dist, time)) | ||
+ | with(hills, plot(time~dist)) | ||
+ | |||
+ | ## cosmética | ||
+ | with(hills, plot(time~dist, | ||
+ | with(hills, plot(time~dist, | ||
+ | |||
+ | ## descobrindo os símbolos | ||
+ | plot(1:25, 1:25, pch=1:25) | ||
+ | ## descobrindo as cores | ||
+ | plot(1:8, 1:8, col=1:8, pch=19, cex=2) | ||
+ | plot(1:20, 1:20, col=terrain.colors(20), | ||
+ | plot(1:10, 1:10, col=heat.colors(10), | ||
+ | plot(1:13, 1:13, col=gray(seq(0, | ||
+ | par(bg=" | ||
+ | plot(1:13, 1:13, col=gray(seq(1, | ||
+ | colors() | ||
+ | par(bg=" | ||
+ | plot(1:13, 1:13, col=gray(seq(1, | ||
+ | par(bg=" | ||
+ | |||
+ | with(hills, plot(time~dist)) | ||
+ | |||
+ | lm.h <- lm(time ~ dist, data=hills) | ||
+ | lm.h | ||
+ | names(lm.h) | ||
+ | is.list(lm.h) | ||
+ | |||
+ | lm.h$coeffi | ||
+ | coef(lm.h) | ||
+ | lm.h$res | ||
+ | resid(lm.h) | ||
+ | lm.h$fitted | ||
+ | fitted(lm.h) | ||
+ | |||
+ | plot(resid(lm.h) ~ fitted(lm.h)) | ||
+ | |||
+ | anova(lm.h) | ||
+ | summary(lm.h) | ||
+ | |||
+ | plot(lm.h) | ||
+ | |||
+ | ## comentários sobre classes, funções genéricas e metodos | ||
+ | methods(plot) | ||
+ | |||
+ | ## dividindo a tela gráfica | ||
+ | par(mfrow=c(2, | ||
+ | plot(lm.h) | ||
+ | par(mfrow=c(1, | ||
+ | |||
+ | |||
+ | ## alguns dispositivos gráficos | ||
+ | jpeg(" | ||
+ | par(mfrow=c(2, | ||
+ | plot(lm.h) | ||
+ | dev.off() | ||
+ | |||
+ | pdf(" | ||
+ | par(mfrow=c(2, | ||
+ | plot(lm.h) | ||
+ | dev.off() | ||
+ | |||
+ | postscript(" | ||
+ | par(mfrow=c(2, | ||
+ | plot(lm.h) | ||
+ | dev.off() | ||
+ | |||
+ | with(hills, plot(time ~ dist)) | ||
+ | |||
+ | |||
+ | lm.h <- lm(time~dist, | ||
+ | |||
+ | ## linhas num gráfico | ||
+ | abline(h=100) | ||
+ | abline(h=c(50, | ||
+ | abline(v=10, | ||
+ | |||
+ | with(hills, plot(time ~ dist, ylim=c(0, 250))) | ||
+ | abline(coef(lm.h)) | ||
+ | |||
+ | with(hills, | ||
+ | x1=dist, y1=time, lty=2))) | ||
+ | title(" | ||
+ | text(2, 250, substitute(hat(beta)[1] == b1, | ||
+ | list(b1=round(coef(lm.h)[2], | ||
+ | | ||
+ | |||
+ | |||
+ | lm0.h <- lm(time ~ dist-1, data=hills) | ||
+ | lm0.h | ||
+ | |||
+ | with(hills, plot(time ~ dist, ylim=c(0, 250), xlab=" | ||
+ | abline(lm.h) | ||
+ | abline(lm0.h, | ||
+ | legend(" | ||
+ | |||
+ | ## mecanismo de ajuda | ||
+ | help(plot) | ||
+ | ## help no navegador | ||
+ | help.start(browser=" | ||
+ | help(plot) | ||
+ | ## procura no seu computador | ||
+ | help.search(" | ||
+ | # procura no site do R | ||
+ | RSiteSearch(" | ||
+ | ## procura um objeto | ||
+ | find(" | ||
+ | find(" | ||
+ | ## pedaco de palavra (nome do objeto) | ||
+ | apropos(" | ||
+ | |||
+ | ## de volta a regressão... | ||
+ | |||
+ | ## predizendo (valores preditos) usado objetos de ajuste de modelos | ||
+ | with(hills, plot(time~dist, | ||
+ | pred.df <- data.frame(dist=seq(0, | ||
+ | pred.h <- predict(lm.h, | ||
+ | pred.h | ||
+ | lines(pred.df$dist, | ||
+ | |||
+ | ## agora acrescentando intervalo de confiança... | ||
+ | predc.h <- predict(lm.h, | ||
+ | dim(predc.h) | ||
+ | head(predc.h) | ||
+ | matlines(pred.df$dist, | ||
+ | ## ... e o intervalo de predição... | ||
+ | predp.h <- predict(lm.h, | ||
+ | matlines(pred.df$dist, | ||
+ | |||
+ | legend(" | ||
+ | |||
+ | |||
+ | ## retirando o ponto mais influente | ||
+ | |||
+ | args(lm) | ||
+ | |||
+ | lmo.h <- lm(time ~dist, data=hills, subset=(dist< | ||
+ | summary(lmo.h) | ||
+ | predo.h <- predict(lmo.h, | ||
+ | |||
+ | with(hills, plot(time~dist, | ||
+ | lines(pred.df$dist, | ||
+ | lines(pred.df$dist, | ||
+ | names(hills) | ||
+ | points(hills[hills$dist> | ||
</ | </ | ||
+ | |||
+ | Mostrando os resultados dos modelos com e sem o ponto mais atípico em dois gráficos separados. | ||
+ | <code R> | ||
+ | par(mfrow=c(2, | ||
+ | with(hills, plot(time~dist, | ||
+ | lines(pred.df$dist, | ||
+ | matlines(pred.df$dist, | ||
+ | matlines(pred.df$dist, | ||
+ | |||
+ | with(hills, plot(time~dist, | ||
+ | points(hills[hills$dist> | ||
+ | lines(pred.df$dist, | ||
+ | matlines(pred.df$dist, | ||
+ | matlines(pred.df$dist, | ||
+ | |||
+ | hist(resid(lm.h)) | ||
+ | hist(resid(lmo.h)) | ||
+ | </ | ||
+ | |||
+ | Regressão múltipla, fórmulas, transformação e comparação e seleção de modelos. | ||
+ | <code R> | ||
+ | ## | ||
+ | head(hills) | ||
+ | |||
+ | lm2.h <- lm(time ~ dist + climb, data=hills) | ||
+ | anova(lm2.h) | ||
+ | summary(lm2.h) | ||
+ | par(mfrow=c(2, | ||
+ | plot(lm2.h) | ||
+ | |||
+ | shapiro.test(resid(lm2.h)) | ||
+ | |||
+ | par(mfrow=c(1, | ||
+ | boxcox(time ~dist+climb, | ||
+ | |||
+ | |||
+ | lm2r.h <- lm(sqrt(time) ~ dist + climb, data=hills) | ||
+ | coef(lm2r.h) | ||
+ | ## ou... aproveitando o modelo previamente definido... | ||
+ | lm2r.h <- update(lm2.h, | ||
+ | coef(lm2r.h) | ||
+ | |||
+ | summary(lm2r.h) | ||
+ | |||
+ | ## modelo retirando variavel climb | ||
+ | lm3r.h <- update(lm2r.h, | ||
+ | coef(lm3r.h) | ||
+ | |||
+ | anova(lm3r.h, | ||
+ | |||
+ | stepAIC(lm(time ~ dist*climb, data=hills)) | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Materiais apresentados e discutidos no curso: ===== | ||
+ | |||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// |