Description Usage Arguments Details Value See Also Examples
View source: R/lmfreq.R View source: R/lmfreq.R
To fit linear models with data grouped in frequency tables.
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, ...)
|
formula |
an object of class |
data |
a data frame that must contain all variables present in
|
freq |
a character string specifying the variable of frequency weights |
tfq |
a |
object |
a |
... |
See Details |
fit |
a |
scale |
not used |
k |
penalty parameter |
x |
a |
digits |
digits |
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.
It returns an object of class lmfreq
, very similar to lm
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.