lmfreq: 'lmfreq' is used to fit linear models with frequency tables

Description Usage Arguments Details Value See Also Examples

View source: R/lmfreq.R View source: R/lmfreq.R

Description

To fit linear models with data grouped in frequency tables.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
lmfreq(formula, data, freq = NULL)

.lmfreq(formula, tfq)

## S3 method for class 'lmfreq'
logLik(object, ...)

## S3 method for class 'lmfreq'
extractAIC(fit, scale = 0, k = 2, ...)

## S3 method for class 'lmfreq'
AIC(object, ..., k = 2)

## S3 method for class 'lmfreq'
nobs(object, ...)

## S3 method for class 'lmfreq'
summary(object, ...)

## S3 method for class 'lmfreq'
print(x, ...)

## S3 method for class 'summary.lmfreq'
print(x, digits = getOption("digits") - 3, ...)

## S3 method for class 'lmfreq'
predict(object, ...)

Arguments

formula

an object of class formula

data

a data frame that must contain all variables present in formula and freq

freq

a character string specifying the variable of frequency weights

tfq

a tablefreq object

object

a lmfreq object

...

See Details

fit

a lmfreq object

scale

not used

k

penalty parameter

x

a lmfreq object

digits

digits

Details

It computes the linear model of a frequency table. See lm for further details.

Any variables in the formula are removed from the data set.

The dot function are for programming purpose. It does not check the data.

Value

It returns an object of class lmfreq, very similar to lm

See Also

tablefreq

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
## Benchmark
if(require(hflights)){
  formula <-  ArrDelay ~ DepDelay   
  print(system.time(a <- lm(formula, data=hflights)))  ## ~0.4 seconds 
  print(system.time(b <- lmfreq(formula, data=hflights))) ## ~0.12 seconds. 4x faster
}

l0 <- lm(Sepal.Length ~ Sepal.Width,iris)
summary(l0)

tfq <- tablefreq(iris[,1:2])
lf <- lmfreq(Sepal.Length ~ Sepal.Width,tfq, freq="freq")
summary(lf)

all.equal(coef(lf),coef(l0))
all.equal(AIC(lf),AIC(l0))

newdata <- data.frame(Sepal.Width=c(1,NA,7))
predict(lf, newdata)

if(require(MASS)){
   stepAIC(lf)
}

system.time(lmfreq(Sepal.Length ~ Sepal.Width,tfq, freq="freq"))
system.time(.lmfreq(Sepal.Length ~ Sepal.Width,tfq)) # Fast

library(dplyr)
igrouped <- iris %>% group_by(Species)
models <- igrouped %>% do(model=lmfreq(Sepal.Length ~ Sepal.Width, .))
coefs <- models %>%
  do(cbind(as.data.frame(rbind(coef(.$model))),
           Species=.$Species))
coefs

## Not run: 
## If data is too granular, benchmark is worst
n <- 10^6
data <- data.frame(y=rnorm(n),x=rnorm(n))
system.time(lm(y~x,data)) ## ~5 seconds
system.time(lmfreq(y~x,data)) ## ~ 15 seconds
system.time(tfq <- tablefreq(data)) ## ~ 5 seconds
nrow(tfq) # same number of rows than original data
system.time(.lmfreq(y~x,tfq)) ## ~ 10 seconds

## End(Not run)

freqweights documentation built on May 29, 2017, 12:01 p.m.