Skip to content

Regression

morota edited this page Apr 25, 2013 · 3 revisions

Fit least squares with fixed slope

x <- runif(100, -3, 3)
y <- 2 + x + rnorm(100)

# y = b + x instead of y = b+ a*x 
lm(y ~ offset(x))
Call:
lm(formula = y ~ offset(x))

Coefficients:
(Intercept)  
      1.950

# y = b + 3*x instead of y = b+ a*x 
lm(y ~ offset(3*x))

Call:
lm(formula = y ~ offset(3 * x))

Coefficients:
(Intercept)  
      1.712  

# fixed intercept to be 5
lm(y~0+x, offset = rep(5,length(x)))

Call:
lm(formula = y ~ 0 + x, offset = rep(5, length(x)))

Coefficients:
    x  
0.83

Polynomial regression

Read R FAQ on this.

#Polynomial of degree 3
fit <- lm(y~I(x) + I(x^2) + I(x^3))
fit2 <- lm(y ~ poly(x, 3))
AIC(fit)
[1] 304.8669
AIC(fit2)
[1] 304.8669

Use subset of observations for regression

y <- as.matrix(swiss[,1])
X <- as.matrix(swiss[,2:6])
str(y)
 num [1:47, 1] 80.2 83.1 92.5 85.8 76.9 76.1 83.8 92.4 82.4 82.9 ...
str(X)
 num [1:47, 1:5] 17 45.1 39.7 36.5 43.5 35.3 70.2 67.8 53.3 45.2 ...
 - attr(*, "dimnames")=List of 2
 ..$ : chr [1:47] "Courtelary" "Delemont" "Franches-Mnt" "Moutier" ...
 ..$ : chr [1:5] "Agriculture" "Examination" "Education" "Catholic" ...

# use observations larger than 50
# option 1
fit <- lm(y~X, subset=y>50)
fit$df
[1] 38

# option 2
fit2 <- lm(y~X, weight=as.numeric(y>50))
fit2$df
[1] 38

lm.fit() and glm.fit()

Seeds dataset is taken from Openbugs.

r <- c(10,23,23,26,17,5,53,55,32,46,10,8,10,8,23,0,3,22,15,32,3)
n <- c(39,62,81,51,39,6,74,72,51,79,13,16,30,28,45,4,12,41,30,51,7)
x0 <- rep(1, length(n))
x1 <- rep( c(0,1), c(11, 10) )
x2 <- c( rep(0, 5, ),  rep(1, 6, ) , rep(0, 5, ), rep(1, 5, ) )
X <- as.matrix(data.frame(x0, x1,x2))
fit2 <- glm.fit(X, cbind(as.numeric(r), as.numeric(n-r)),family=binomial())

lsfit()

y <- rnorm(10)
x <- matrix(c(rnorm(30)), ncol=3)
lsfit(x,y)

do.call() with lm()

args <- list(data=cars, formula=dist~speed);
do.call("lm",args);

Clone this wiki locally