Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Codigo minimo reproduzivel em R
## Este exemplo provê um código em R que outros usuários podem reproduzir
## para facilitar a solução de problemas observados.
##
## Na R-br em: http://r-br.2285057.n4.nabble.com/R-br-Produzindo-um-Codigo-Minimo-Reproduzivel-CMR-td3674188.html
#################################################################
## PASSO 1: Carregue os pacotes necessários no início do código
#################################################################
library(splines)
#################################################################
## PASSO 2: Geração de dados:
#################################################################
## - Opção 1: gere dados aleatórios fixando a semente:
#################################################################
set.seed(1)
meusDados1 <- rnorm(10)
#################################################################
## - Opção 2: gere os dados no seu computador e use 'dput()'
## para obter sua estrutura; o resultado de 'dput()' deve ser
## assinalado å variável de interesse:
#################################################################
meusDados1 <- c(-0.626453810742332, 0.183643324222082, -0.835628612410047,
1.59528080213779, 0.329507771815361, -0.820468384118015, 0.487429052428485,
0.738324705129217, 0.575781351653492, -0.305388387156356)
#################################################################
## PASSO 3:
#################################################################
## - Parte 1: Use nomes apropriados para variáveis:
#################################################################
t <- mean(meusDados1) ## nome ruim pq confunde com comando t()
muDados1 <- mean(meusDados1) ## melhor explicado...
#################################################################
## - Parte 2: Comente o código! As pessoas que tentam ajudar também têm outros afazeres
## O exemplo abaixo é ruim, pois não possui nenhuma explicação do que o usuário tenta fazer.
#################################################################
tmp1 <- NULL
tmp2 <- NULL
for (i in 1:length(meusDados1)){
if (meusDados1[i] < 0){
tmp1 <- c(tmp1, meusDados1[i])
}else{
tmp2 <- c(tmp2, meusDados1[i])
}
}
muPos <- mean(tmp2)
muNeg <- mean(tmp1)
base1 <- ns(tmp2-muPos, 3)
base2 <- ns(tmp1-muNeg, 3)
#################################################################
## O exemplo abaixo é bem melhor, pois contém explicações:
#################################################################
### 'tmp1' conterá os valores negativos dos dados
tmp1 <- NULL
### 'tmp2' conterá os valores positivos dos dados
tmp2 <- NULL
### filtrando os dados e os colocando nas devidas variáveis
for (i in 1:length(meusDados1)){
if (meusDados1[i] < 0){
tmp1 <- c(tmp1, meusDados1[i])
}else{
tmp2 <- c(tmp2, meusDados1[i])
}
}
### determinando as médias para cada uma das variáveis.
muPos <- mean(tmp2)
muNeg <- mean(tmp1)
### gerando bases via splines
base1 <- ns(tmp2-muPos, 3)
base2 <- ns(tmp1-muNeg, 3)
#################################################################
## PASSO 4: Informe o resultado de 'sessionInfo()' como comentário
#################################################################
##> sessionInfo()
##R version 2.13.0 RC (2011-04-08 r55386)
##Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
##
##locale:
##[1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8
##
##attached base packages:
##[1] splines stats graphics grDevices utils datasets methods
##[8] base
#################################################################
## PASSO 5: Aguarde uma resposta na lista...
#################################################################
## Fulano, abaixo segue uma solução mais eficiente para seu código. b
base1b <- ns(scale(meusDados1[meusDados1 > 0], scale=FALSE), 3)
base2b <- ns(scale(meusDados1[meusDados1 < 0], scale=FALSE), 3)
identical(base1, base1b) ## TRUE
identical(base2, base2b) ## TRUE
## Adaptado de um documento do Hadley: https://gist.github.com/270442
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.