Diferenças
Aqui você vê as diferenças entre duas revisões dessa página.
| Ambos lados da revisão anteriorRevisão anteriorPróxima revisão | Revisão anterior | ||
| pessoais:jcfaria [2007/02/28 11:59] – jcfaria | pessoais:jcfaria [2012/08/01 19:25] (atual) – jcfaria | ||
|---|---|---|---|
| Linha 1: | Linha 1: | ||
| - | ====== | + | ====== José Cláudio Faria ====== |
| - | {{pessoais: | + | {{ pessoais: |
| - | Na Praia do Sul de Ilhéus/Bahia (janeiro de 2007) refletindo profundamente sobre o R!!! | + | |
| + | Eu na Praia do Sul de Ilhéus/BA, em janeiro de 2007, refletindo profundamente sobre estatística computacional e o R!!! | ||
| + | |||
| + | Brincadeiras a parte... | ||
| + | |||
| + | **1. Quem sou** | ||
| + | |||
| + | - Engenheiro Agrônomo | ||
| + | - Mestrado e Doutorado em Produção Vegetal pela Universidade Federal de Viçosa - UFV/MG | ||
| + | - Pós-doc em estatística e experimentação agronômica (ESALQ) | ||
| + | |||
| + | |||
| + | **2. O que tenho feito profissionalmente** | ||
| + | |||
| + | - Professor de estatística e pesquisador da Universidade Estadual de Santa Cruz - UESC/BA; | ||
| + | |||
| + | - Tenho estado desenvolvendo algumas soluções computacionais voltadas para o ambiente R: | ||
| + | - Editores: | ||
| + | - Tinn-R ([[http:// | ||
| + | - Vim-R-plugin ([[http:// | ||
| + | - Pacotes: | ||
| + | - bpca ([[http:// | ||
| + | - TinnR ([[http:// | ||
| + | - fdth ([[http:// | ||
| + | - ScottKnott ([[http:// | ||
| + | - TukeyC ([[http:// | ||
| + | |||
| + | |||
| + | **3. Sobre o R** | ||
| + | |||
| + | - Gostaria de tê-lo encontrado desde o início de minha carreira na área de estatística computacional! | ||
| + | |||
| + | |||
| + | **4. Sobre o futuro** | ||
| + | |||
| + | - Desejo aprofundar os conhecimentos em análise multivariada de dados no ambiente R; | ||
| + | |||
| + | - Trocar experiências com pessoas e equipes envolvidas nestas áreas. | ||
| ===== Tinn-R ===== | ===== Tinn-R ===== | ||
| Linha 17: | Linha 54: | ||
| * {{pessoais: | * {{pessoais: | ||
| - | ===== Materiais | + | ===== Materiais sobre o R ===== |
| ==== Scripts ==== | ==== Scripts ==== | ||
| + | Todos os usuários estão automaticamente convidados a darem sugestões e alterarem contrutivamente todas as funções e scripts desta página. Solicito a gentileza de me enviar um [[joseclaudio.faria@terra.com.br | email]] comunicando as alterações. | ||
| + | |||
| === Introdução ao R === | === Introdução ao R === | ||
| Linha 558: | Linha 597: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | ==== Funções úteis ==== | ||
| + | === Superfície de resposta === | ||
| + | == Função plotlm3d == | ||
| + | The simple, power and very flexible function **plotlm3d** enables you to plot 3d points and/or surfaces obtained from linear methods. It was adapted from scatter3d [[http:// | ||
| + | |||
| + | It requires the **rgl** package that you can download from [[http:// | ||
| + | |||
| + | < | ||
| + | # | ||
| + | # Name : plotlm3d | ||
| + | # Original author: John Fox (scatter3d from package Rcmdr) | ||
| + | # Changes | ||
| + | # Date (dd/mm/yy): 12/8/06 19:44:37 | ||
| + | # Version | ||
| + | # Aim : To plot 3d scatter, an or, surfaces with rgl package | ||
| + | # | ||
| + | |||
| + | # Arguments: | ||
| + | # x | ||
| + | # y | ||
| + | # z | ||
| + | # surface | ||
| + | # model one or more linear model to fit ('z ~ x + y' is the default). | ||
| + | # groups | ||
| + | # a different surface or set of surfaces is plotted for each | ||
| + | # level of the factor; in this event, the colours in plane.col | ||
| + | # are used successively for the points and surfaces. | ||
| + | # model.by.group | ||
| + | # of groups; the order of the models must be the same of the | ||
| + | # level of the. | ||
| + | # model.summary | ||
| + | # simple.axes | ||
| + | # box | ||
| + | # xlab, | ||
| + | # ylab, | ||
| + | # zlab axis labels. | ||
| + | # surface.col | ||
| + | # | ||
| + | # point.col | ||
| + | # grid.col | ||
| + | # grid plot grid lines on the regression surface(s) (TRUE or FALSE). | ||
| + | # grid.lines | ||
| + | # the x and z directions. | ||
| + | # sphere.factor | ||
| + | # | ||
| + | # threshold | ||
| + | # | ||
| + | # speed | ||
| + | # revolutions | ||
| + | |||
| + | plotlm3d <- function (x, y, z, | ||
| + | surface | ||
| + | model = 'z ~ x + y', | ||
| + | groups | ||
| + | model.by.group = F, | ||
| + | model.summary | ||
| + | simple.axes | ||
| + | box = F, | ||
| + | xlab = deparse(substitute(x)), | ||
| + | ylab = deparse(substitute(y)), | ||
| + | zlab = deparse(substitute(z)), | ||
| + | surface.col | ||
| + | ' | ||
| + | point.col | ||
| + | grid.col | ||
| + | grid = T, | ||
| + | grid.lines | ||
| + | sphere.factor | ||
| + | threshold | ||
| + | speed = 0.5, | ||
| + | revolutions | ||
| + | { | ||
| + | require(rgl) | ||
| + | require(mgcv) | ||
| + | summaries <- list() | ||
| + | |||
| + | if ((!is.null(groups)) && model.by.group) | ||
| + | if (!nlevels(groups) == length(model)) | ||
| + | stop(' | ||
| + | |||
| + | if ((!is.null(groups)) && (nlevels(groups) > length(surface.col))) | ||
| + | stop(' | ||
| + | |||
| + | if ((!is.null(groups)) && (!is.factor(groups))) | ||
| + | stop(' | ||
| + | |||
| + | xlab; ylab; zlab | ||
| + | |||
| + | valid <- if (is.null(groups)) | ||
| + | complete.cases(x, | ||
| + | else | ||
| + | complete.cases(x, | ||
| + | |||
| + | x <- x[valid] | ||
| + | y <- y[valid] | ||
| + | z <- z[valid] | ||
| + | | ||
| + | if (!is.null(groups)) | ||
| + | groups <- groups[valid] | ||
| + | |||
| + | levs <- levels(groups) | ||
| + | size <- max(c(x, | ||
| + | |||
| + | if (is.null(groups)) { | ||
| + | if (size > threshold) | ||
| + | spheres3d(x, | ||
| + | else | ||
| + | points3d(x, y, z, color = point.col) | ||
| + | } | ||
| + | else { | ||
| + | if (size > threshold) | ||
| + | spheres3d(x, | ||
| + | else | ||
| + | points3d(x, y, z, color = surface.col[as.numeric(groups)]) | ||
| + | } | ||
| + | |||
| + | aspect3d(c(1, | ||
| + | |||
| + | if (surface) { | ||
| + | xvals <- seq(min(x), max(x), length = grid.lines) | ||
| + | yvals <- seq(min(y), max(y), length = grid.lines) | ||
| + | | ||
| + | dat <- expand.grid(x = xvals, y = yvals) | ||
| + | |||
| + | for (i in 1: | ||
| + | if (is.null(groups)) { | ||
| + | mod <- lm(formula(model[i])) | ||
| + | |||
| + | if (model.summary) | ||
| + | summaries[[model[i]]] <- summary(mod) | ||
| + | |||
| + | zhat <- matrix(predict(mod, | ||
| + | surface3d(xvals, | ||
| + | |||
| + | if (grid) | ||
| + | surface3d(xvals, | ||
| + | lit = F, front = ' | ||
| + | } | ||
| + | else { # groups is not NULL | ||
| + | if (!model.by.group) { | ||
| + | for (j in 1: | ||
| + | mod <- lm(formula(model[i]), | ||
| + | |||
| + | if (model.summary) | ||
| + | summaries[[paste(model[i], | ||
| + | |||
| + | zhat <- matrix(predict(mod, | ||
| + | surface3d(xvals, | ||
| + | |||
| + | if (grid) | ||
| + | | ||
| + | lit = F, front = ' | ||
| + | |||
| + | texts3d(min(x), | ||
| + | groups = levs[j])), paste(levs[j], | ||
| + | } | ||
| + | } | ||
| + | else { # model.by.group is TRUE | ||
| + | mod <- lm(formula(model[i]), | ||
| + | |||
| + | if (model.summary) | ||
| + | summaries[[paste(model[i], | ||
| + | |||
| + | zhat <- matrix(predict(mod, | ||
| + | |||
| + | surface3d(xvals, | ||
| + | |||
| + | if (grid) | ||
| + | surface3d(xvals, | ||
| + | lit = F, front = ' | ||
| + | |||
| + | texts3d(min(x), | ||
| + | groups = levs[i])), paste(levs[i], | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | if(simple.axes) { | ||
| + | axes3d(c(' | ||
| + | title3d(xlab = xlab, ylab = ylab, zlab = zlab) | ||
| + | } | ||
| + | else | ||
| + | decorate3d(xlab = xlab, ylab = ylab, zlab = zlab, box = box) | ||
| + | |||
| + | if (revolutions > 0) { | ||
| + | start <- proc.time()[3] | ||
| + | startMatrix <- par3d(" | ||
| + | while ((theta <- speed*(proc.time()[3] - start))/ | ||
| + | rgl.viewpoint(userMatrix = rotate3d(startMatrix, | ||
| + | } | ||
| + | } | ||
| + | if (model.summary) | ||
| + | return(summaries) | ||
| + | else | ||
| + | return(invisible(NULL)) | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | == Usando a função plotlm3d == | ||
| + | < | ||
| + | # | ||
| + | # Name : Script to test plotlm3d | ||
| + | # Author | ||
| + | # Date (dd/mm/yy): 2012/07/01 | ||
| + | # Version | ||
| + | # Aim : To plot 3d scatter, an or, surfaces with rgl package | ||
| + | # | ||
| + | |||
| + | # mtrace(plotlm3d) | ||
| + | # mtrace.off | ||
| + | |||
| + | # Example 1 | ||
| + | open3d() | ||
| + | rgl.bringtotop(stay = T) | ||
| + | with(iris, plotlm3d(Sepal.Length, | ||
| + | surface | ||
| + | groups | ||
| + | xlab = ' | ||
| + | ylab = ' | ||
| + | zlab = ' | ||
| + | grid = F, | ||
| + | sphere.factor = 1)) | ||
| + | |||
| + | # Example 2 | ||
| + | open3d() | ||
| + | rgl.bringtotop(stay = T) | ||
| + | with(iris, plotlm3d(Sepal.Length, | ||
| + | model = c('z ~ x + y', | ||
| + | 'z ~ x + y + I(x^2) + I(y^2) + I(x*y)' | ||
| + | surface | ||
| + | groups | ||
| + | simple.axes | ||
| + | box = T, | ||
| + | xlab = ' | ||
| + | ylab = ' | ||
| + | zlab = ' | ||
| + | grid = F, | ||
| + | sphere.factor = 1)) | ||
| + | |||
| + | # Example 3 | ||
| + | open3d() | ||
| + | rgl.bringtotop(stay = T) | ||
| + | with(iris, plotlm3d(Sepal.Length, | ||
| + | model = c('z ~ x + y', | ||
| + | 'z ~ x + y + I(x^2) + I(y^2) + I(x*y)' | ||
| + | surface | ||
| + | xlab = ' | ||
| + | ylab = ' | ||
| + | zlab = ' | ||
| + | grid = F, | ||
| + | sphere.factor = 1)) | ||
| + | |||
| + | # Example 4 | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | 'z ~ x + y + I(x^2) + I(y^2) + I(x*y)', | ||
| + | 'z ~ I(x^3) + I(y^3)' | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | # Example 5: Netter | ||
| + | x = c( 274, 180, 375, 205, | ||
| + | | ||
| + | y = c(2450, 3254, 3802, 2838, 2347, 3782, 3008, 2450, 2137, 2560, | ||
| + | 4020, 4427, 2660, 2088, 2605) | ||
| + | z = c( 162, 120, 223, 131, | ||
| + | | ||
| + | |||
| + | mreg = lm(z ~ x + y) | ||
| + | ndata = data.frame(x = c(150, 274, 220, 370), y = c(4000, 2800, 3500, 3100)) | ||
| + | zpred = predict(mreg, | ||
| + | |||
| + | open3d() | ||
| + | rgl.bringtotop(stay = T) | ||
| + | plotlm3d(x, y, z, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | spheres3d(x = c(150, 274, 220, 370), y = c(4000, 2800, 3500, 3100), zpred, | ||
| + | col = ' | ||
| + | </ | ||
| + | |||