Gracias por la respuesta! El código tuyo funciona sin problema, pero cuando lo "adapto" me da error:
> ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) + trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) + group <- gl(2, 10, 20, labels = c("Ctl","Trt")) + weight <- c(ctl, trt) + df <- data.frame (weight = weight, group = group) + rm(ctl, trt, group, weight) ## Ya que la regresión se ejecutaria, por ejemplo, dentro de otro espacio y estos objetos no estarian accesibles + model <- "df$weight ~ df$group" + summary (lm(model)) Error in df$weight : object of type 'closure' is not subsettable Que estoy haciendo mal? Muchas gracias y saludos! On Thu, 18 Nov 2021 13:12:43 +0100 Proyecto R-UCA <r-...@uca.es> wrote: > Buenas, > > a ver si esto te sirve: > ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) > trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) > group <- gl(2, 10, 20, labels = c("Ctl","Trt")) > weight <- c(ctl, trt) > dd <- data.frame(weight = weight, group = group, ctl = ctl, trt=trt) > model <- "dd$weight ~ dd$group" > summary(lm(model)) > > Únicamente tendrías que construir previamente la fórmula con paste. > > La salida es: > Call: > lm(formula = model) > > Residuals: > Min 1Q Median 3Q Max > -1.0710 -0.4938 0.0685 0.2462 1.3690 > > Coefficients: > Estimate Std. Error t value Pr(>|t|) > (Intercept) 5.0320 0.2202 22.850 9.55e-15 *** > dd$groupTrt -0.3710 0.3114 -1.191 0.249 > --- > Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 > > Residual standard error: 0.6964 on 18 degrees of freedom > Multiple R-squared: 0.07308, Adjusted R-squared: 0.02158 > F-statistic: 1.419 on 1 and 18 DF, p-value: 0.249 > > Un saludo. > > El jue, 18-11-2021 a las 12:03 +0100, Griera escribió: > > Hola, buenos días: > > > > No es un problema concreto que tenga ahora, pero es un problema general > > que no se si tiene solución fácil. Hago una regresión (de lm.html): > > > > ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) > > trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) > > group <- gl(2, 10, 20, labels = c("Ctl","Trt")) > > weight <- c(ctl, trt) > > lm.D9 <- lm(weight ~ group) > > > > Si quiero que la variable independiente sea una "variable", y hago: > > > > X = "group" > > lm.D9 <- lm(weight ~ X) > > > > Y da el error: Error in model.frame.default(formula = XVD ~ group, > > drop.unused.levels = TRUE) : > > variable lengths differ (found for 'group') > > > > Ya que, como me decían el otro día "estás asignando a la variable X el > > valor "X" y no el > > contenido de la variable X." > > > > ¿No hay forma de que entienda que "X" es el nombre de la variable > > independiente "group", que no sea pasar la posición de la variable? > > > > Muchas gracias y saludos a todos. > > > > _______________________________________________ > > R-help-es mailing list > > R-help-es@r-project.org > > https://stat.ethz.ch/mailman/listinfo/r-help-es > > _______________________________________________ > R-help-es mailing list > R-help-es@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-help-es _______________________________________________ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es