regression

Fitting a linear model with multiple LHS

送分小仙女□ 提交于 2019-11-25 23:50:04
问题 I am new to R and I want to improve the following script with an *apply function (I have read about apply , but I couldn\'t manage to use it). I want to use lm function on multiple independent variables (which are columns in a data frame). I used for (i in (1:3) { assign(paste0(\'lm.\',names(data[i])), lm(formula=formula(i),data=data)) } Formula(i) is defined as formula=function(x) { as.formula ( paste(names(data[x]),\'~\', paste0(names(data[-1:-3]), collapse = \'+\')), env=parent.frame() ) }

Linear Regression and group by in R

冷暖自知 提交于 2019-11-25 22:32:24
问题 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 states). I want to fit a regression for each state so that at the end I have a vector of lm responses. I can imagine doing for loop for each state then doing the regression inside the loop and adding the results of each regression to a vector. That does not seem very R-like, however. In SAS I would do a \'by\' statement and in SQL I would

How to debug “contrasts can be applied only to factors with 2 or more levels” error?

喜夏-厌秋 提交于 2019-11-25 21:57:11
问题 Here are all the variables I\'m working with: str(ad.train) $ Date : Factor w/ 427 levels \"2012-03-24\",\"2012-03-29\",..: 4 7 12 14 19 21 24 29 31 34 ... $ Team : Factor w/ 18 levels \"Adelaide\",\"Brisbane Lions\",..: 1 1 1 1 1 1 1 1 1 1 ... $ Season : int 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 ... $ Round : Factor w/ 28 levels \"EF\",\"GF\",\"PF\",..: 5 16 21 22 23 24 25 26 27 6 ... $ Score : int 137 82 84 96 110 99 122 124 49 111 ... $ Margin : int 69 18 -56 46 19 5 50 69 -26