Applying the function to lm ()

I'm new to R, and I just learn about functions applyand how they work. I just want to extract the coefficients from the set lmfor the variable x by product color and brand in a few years.

I know that I can create a for loop and a subset of the model year data and customize it, but I think it's time to use more built-in functions, so I want to be able to do this with a function or one of the applicable functions. Here is what I thought.

#some made up data

x<-rnorm(50,13400,1200)
color<-sample(factor(c("Red","Black","Blue","Green","White")),50,replace=T)
year<-sample(factor(2006:2012),50,replace=T)
brand<-sample(factor(c("A","B","C","D")),50,replace=T)

d<-data.frame(x,color,year,brand)

#now I want to fit the model lm(x~color+brand) for each year level
#this is what I was thinking...

tmp<-with(d,by(x,year,function(y) lm(x~color,data=y)))
sapply(tmp,coef)

Error in eval (predvars, data, env): numeric 'envir' arg not 1 long

I base this on exapmle R when I introduced help(by)

+5
source share
3 answers

Try this instead:

tmp <- by(d, year, function(d.subset) lm(x~color, data=d.subset))
+5
source

R ( , , ), , , , . - , , , , .

lme4 lmList. ?lmList,

(fm1 <- lmList(Reaction ~ Days | Subject, sleepstudy))

Reaction ~ Days Subject. , fm1:

> coef(fm1)
    (Intercept)      Days
308    244.1927 21.764702
309    205.0549  2.261785
310    203.4842  6.114899
330    289.6851  3.008073
331    285.7390  5.266019
332    264.2516  9.566768
333    275.0191  9.142045
334    240.1629 12.253141
335    263.0347 -2.881034
337    290.1041 19.025974
349    215.1118 13.493933
350    225.8346 19.504017
351    261.1470  6.433498
352    276.3721 13.566549
369    254.9681 11.348109
370    210.4491 18.056151
371    253.6360  9.188445
372    267.0448 11.298073

( id Subjects). . ?lmList, , , , ..

+5

This is much simpler in more modern packages, for example. data.table:

library(data.table)
setDT(d)
d[ , .(reg = list(lm(x ~ color))), by = year]
#    year  reg
# 1: 2012 <lm>
# 2: 2006 <lm>
# 3: 2011 <lm>
# 4: 2008 <lm>
# 5: 2007 <lm>
# 6: 2010 <lm>
# 7: 2009 <lm>

There regare lmobjects in the column ; note that we need to wrap lmin list(.)so as data.tablenot to confuse a simple list (note that is.list(lm(x ~ color, data = d))there is TRUE.

+1
source

All Articles