lm

Format of R's lm() Formula with a Transformation

自古美人都是妖i 提交于 2019-12-22 05:39:32
问题 I can't quite figure out how to do the following in one line: data(attenu) x_temp = attenu$accel^(1/4) y_temp = log(attenu$dist) best_line = lm(y_temp ~ x_temp) Since the above works, I thought I could do the following: data(attenu) best_line = lm( log(attenu$dist) ~ (attenu$accel^(1/4)) ) But this gives the error: Error in terms.formula(formula, data = data) : invalid power in formula There's obviously something I'm missing when using transformed variables in R's formula format. Why doesn't

Plotting one predictor of a model that has several predictors with ggplot

折月煮酒 提交于 2019-12-21 21:43:08
问题 Here is an typical example of linear model and a ggplot: require(ggplot2) utils::data(anorexia, package = "MASS") anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt), family = gaussian, data = anorexia) coef(anorex.1) (Intercept) Prewt TreatCont TreatFT 49.7711090 -0.5655388 -4.0970655 4.5630627 ggplot(anorexia, aes(y=Postwt, x=Prewt)) + geom_point() + geom_smooth(method='lm', se=F) My problem is that the regression that is made by geom_smooth(...) is not the same model than anorex.1 but

R: polynomial shortcut notation in nls() formula

馋奶兔 提交于 2019-12-21 05:35:54
问题 With the linear model function lm() polynomial formulas can contain a shortcut notation like this: m <- lm(y ~ poly(x,3)) this is a shortcut that keeps the user from having to create x^2 and x^3 variables or typing them in the formula like I(x^2) + I(x^3) . Is there comparable notation for the nonlinear function nls() ? 回答1: poly(x, 3) is rather more than just a shortcut for x + I(x ^ 2) + I(x ^ 3) - it actually produces legendre polynomials which have the nice property of being uncorrelated:

How to remove a lower order parameter in a model when the higher order parameters remain?

旧城冷巷雨未停 提交于 2019-12-21 04:24:07
问题 The problem: I cannot remove a lower order parameter (e.g., a main effects parameter) in a model as long as the higher order parameters (i.e., interactions) remain in the model. Even when doing so, the model is refactored and the new model is not nested in the higher model. See the following example (as I am coming from ANOVAs I use contr.sum ): d <- data.frame(A = rep(c("a1", "a2"), each = 50), B = c("b1", "b2"), value = rnorm(100)) options(contrasts=c('contr.sum','contr.poly')) m1 <- lm

How to predict x values from a linear model (lm)

孤者浪人 提交于 2019-12-20 19:44:56
问题 I have this data set: x <- c(0, 40, 80, 120, 160, 200) y <- c(6.52, 5.10, 4.43, 3.99, 3.75, 3.60) I calculated a linear model using lm() : model <- lm(y ~ x) I want know the predicted values of x if I have new y values, e.g. ynew <- c(5.5, 4.5, 3.5) , but if I use the predict() function, it calculates only new y values. How can I predict new x values if I have new y values? 回答1: Since this is a typical problem in chemistry (predict values from a calibration), package chemCal provides inverse

How to predict x values from a linear model (lm)

强颜欢笑 提交于 2019-12-20 19:44:07
问题 I have this data set: x <- c(0, 40, 80, 120, 160, 200) y <- c(6.52, 5.10, 4.43, 3.99, 3.75, 3.60) I calculated a linear model using lm() : model <- lm(y ~ x) I want know the predicted values of x if I have new y values, e.g. ynew <- c(5.5, 4.5, 3.5) , but if I use the predict() function, it calculates only new y values. How can I predict new x values if I have new y values? 回答1: Since this is a typical problem in chemistry (predict values from a calibration), package chemCal provides inverse

Running a stepwise linear model with BIC criterion

∥☆過路亽.° 提交于 2019-12-20 18:31:06
问题 Is it possible to set a stepwise linear model to use the BIC criteria rather than AIC? I've been trying this but it still calculates each step using AIC values rather than BIC null = lm(data[,1] ~ 1) full = lm(data[,1] ~ age + bmi + gender + group) step(null, scope = list(lower=null,upper=full), direction="both", criterion = "BIC") 回答1: Add the argument k=log(n) to the step function ( n number of samples in the model matrix) From ?step : Arguments: ... k the multiple of the number of degrees

Interpreting regression coefficients in R [closed]

时间秒杀一切 提交于 2019-12-20 06:39:26
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I'm trying to fit a x*log(x) model to the data. The fitting is performed successfully but I have difficulties in interpreting the resulting coefficients. Here a snapshot of my code. x <- c(6, 11, 16, 21, 26, 31, 36, 41, 46, 51) y <- c(5.485, 6.992, 7.447, 8.134, 8.524, 8.985, 9.271, 9.647, 10.561, 9.971) fit <-

How can I force dropping intercept or equivalent in this linear model?

若如初见. 提交于 2019-12-20 06:21:58
问题 Consider the following table : DB <- data.frame( Y =rnorm(6), X1=c(T, T, F, T, F, F), X2=c(T, F, T, F, T, T) ) Y X1 X2 1 1.8376852 TRUE TRUE 2 -2.1173739 TRUE FALSE 3 1.3054450 FALSE TRUE 4 -0.3476706 TRUE FALSE 5 1.3219099 FALSE TRUE 6 0.6781750 FALSE TRUE I'd like to explain my quantitative variable Y by two binary variables (TRUE or FALSE) without intercept. The argument of this choice is that, in my study, we can't observe X1=FALSE and X2=FALSE at the same time, so it doesn't make sense

plot regression line in R

被刻印的时光 ゝ 提交于 2019-12-20 06:09:15
问题 I want to plot a simple regression line in R. I've entered the data, but the regression line doesn't seem to be right. Can someone help? x <- c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120) y <- c(10, 18, 25, 29, 30, 28, 25, 22, 18, 15, 11, 8) df <- data.frame(x,y) plot(y,x) abline(lm(y ~ x)) 回答1: Oh, @GBR24 has nice formatted data. Then I'm going to elaborate a little bit based on my comment. fit <- lm(y ~ poly(x, 3)) ## polynomial of degree 3 plot(x, y) ## scatter plot (colour: black)