Linear Regression and group by in R

前端 未结 10 1511
抹茶落季
抹茶落季 2020-11-22 02:27

I want to do a linear regression in R using the lm() function. My data is an annual time series with one field for year (22 years) and another for state (50 sta

10条回答
  •  情话喂你
    2020-11-22 02:55

    The lm() function above is an simple example. By the way, I imagine that your database has the columns as in the following form:

    year state var1 var2 y...

    In my point of view, you can to use the following code:

    require(base) 
    library(base) 
    attach(data) # data = your data base
                 #state is your label for the states column
    modell<-by(data, data$state, function(data) lm(y~I(1/var1)+I(1/var2)))
    summary(modell)
    

提交回复
热议问题