
Uma dúvida Eu estou escrevendo um código utilizando o R panel. Ele é uma extensão do código escrito pelo Walmes (no blog ridículas), no qual pretendo adaptá-lo para trabalhar com mais modelos. No código abaixo pretendo fornecer interfaces para a importação dos dados pelo usuário. O nome das variáveis dependente e independente utilizada no banco de dados importado deve ser fornecida pelo usuário e o código substitui o nome destas variáveis por y e x para processamento. Acontece que a função "var_nome" ela captura os nomes fornecidos pelo usuário, no entanto não estou conseguindo realizar a mudança dos respectivos nomes na base de dados. O comando colnames(da)[grep( var_dep,colnames(da))]<-"y" funciona fora da função, mas não parece funcionar dentro da função, uma vez que após rodar os comando abaixo e digitar head(da), os nomes das colunas não são alterados. No exemplo abaixo as colunas PG e Tempo, devem ser substituídas por Y e X respectivamente Como posso resolver isso? require(rpanel) database<-function(panel){ da<-read.csv(file.choose(), header=TRUE) return(panel) } #----------------------------------------------------------------------------- var_nome<-function(panel){ var_dep<<-panel$var_y var_indep<<-panel$var_x colnames(da)[grep(var_dep,colnames(da))]<-"y" colnames(da)[grep(var_indep,colnames(da))]<-"x" return(panel) } panel <- rp.control() #botão para importar banco de dados rp.button(panel, action=database, title="importar dados") # Obtem os nomes das variáveis dependentes rp.textentry(panel=panel, variable=var_y, labels="nome da variável dependente(y):", initval="", action=var_nome) rp.textentry(panel=panel, variable=var_x, labels="nome da variável independente(x):", initval="", action=var_nome) dput(da[1:134,]) da<-structure(list(Tubo = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 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L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), concentracao = c(20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 40L), Tempo = c(1L, 2L, 3L, 4L, 5L, 6L, 9L, 12L, 18L, 24L, 30L, 36L, 48L, 60L, 72L, 84L, 96L, 120L, 144L, 1L, 2L, 3L, 4L, 5L, 6L, 9L, 12L, 18L, 24L, 30L, 36L, 48L, 60L, 72L, 84L, 96L, 120L, 144L, 1L, 2L, 3L, 4L, 5L, 6L, 9L, 12L, 18L, 24L, 30L, 36L, 48L, 60L, 72L, 84L, 96L, 120L, 144L, 1L, 2L, 3L, 4L, 5L, 6L, 9L, 12L, 18L, 24L, 30L, 36L, 48L, 60L, 72L, 84L, 96L, 120L, 144L, 1L, 2L, 3L, 4L, 5L, 6L, 9L, 12L, 18L, 24L, 30L, 36L, 48L, 60L, 72L, 84L, 96L, 120L, 144L, 1L, 2L, 3L, 4L, 5L, 6L, 9L, 12L, 18L, 24L, 30L, 36L, 48L, 60L, 72L, 84L, 96L, 120L, 144L, 1L, 2L, 3L, 4L, 5L, 6L, 9L, 12L, 18L, 24L, 30L, 36L, 48L, 60L, 72L, 84L, 96L, 120L, 144L, 1L), PG = c(4.18, 6.16, 8.13, 9.94, 11.74, 13.72, 17.95, 20.77, 21.16, 21.16, 21.16, 21.16, 21.16, 21.16, 21.16, 21.16, 21.16, 21.16, 21.16, 3.56, 6.16, 9.77, 12.98, 15.35, 16.76, 20.15, 21.72, 22.51, 22.51, 22.51, 22.51, 22.51, 22.51, 22.51, 22.51, 22.51, 22.51, 22.51, 4.8, 8.81, 12.42, 14.62, 16.82, 18, 20.37, 21.78, 22.18, 22.18, 22.18, 22.18, 22.18, 22.18, 22.18, 22.18, 22.18, 22.18, 22.18, 3.96, 7.96, 12.19, 14.17, 15.97, 17.38, 19.58, 20.99, 21.95, 21.95, 21.95, 21.95, 21.95, 21.95, 21.95, 21.95, 21.95, 21.95, 21.95, 4.41, 8.02, 11.23, 13.43, 15.41, 16.82, 19.02, 20.6, 20.99, 20.99, 20.99, 20.99, 20.99, 20.99, 20.99, 20.99, 20.99, 20.99, 20.99, 4.58, 8.19, 11.01, 12.59, 14.39, 15.58, 17.55, 19.13, 20.09, 20.09, 20.09, 20.09, 20.09, 20.09, 20.09, 20.09, 20.09, 20.09, 20.09, 4.8, 8.19, 10.95, 12.93, 14.73, 16.31, 18.51, 20.09, 20.88, 20.88, 20.88, 20.88, 20.88, 20.88, 20.88, 20.88, 20.88, 20.88, 20.88, 4.58)), .Names = c("Tubo", "Amostra", "dieta", "concentracao", "Tempo", "PG"), row.names = c(NA, 134L), class = "data.frame") Em 27 de fevereiro de 2016 09:16, Éder Comunello <comunello.eder@gmail.com> escreveu:
Senhores, bom dia!
Realmente o script disponibilizado pelo Walmes é muito bom. É pra aprender o caminho das pedras!
Mas me surgiu uma ideia... Supondo que se queira apenas visualizar o ajuste do modelo, talvez bastasse utilizar predict() e depois adicionar os resultados ao gráfico.
### <code r> head(Orange, 3) mForm <- as.formula("circumference ~ A/(1+exp((B-age)/C))"); mForm; class(mForm) mExpr <- mForm[[3]]; mExpr; class(mExpr) # A/(1 + exp((B - age)/C))
fm1 <- nls(mForm, Orange, start=list(A=160, B=700, C=350))
new <- seq(xLim[1], xLim[2], len=101) pre1 <- predict(fm1, newdata=list(age=new))
plot(circumference~age, Orange) lines(new, pre1, col=2) # title(mForm) title(mExpr) ### </code>
================================================ Éder Comunello Agronomist (UEM), MSc in Environ. Sciences (UEM) DSc in Agricultural Systems Engineering (USP/Esalq) Brazilian Agricultural Research Corporation (Embrapa) Dourados, MS, Brazil |<O>| ================================================ GEO, -22.2752, -54.8182, 408m UTC-04:00 / DST: UTC-03:00
_______________________________________________ R-br mailing list R-br@listas.c3sl.ufpr.br https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e forneça código mínimo reproduzível.
-- ======================================================================= Fernando Souza Zootecnista, DSc. Produção Animal e-mail:nandodesouza@gmail.com https://producaoanimalcomr.wordpress.com/ ========================================================================