0

Hi I have the following dataframe (with 20 additional columns):

F1<-data.frame(Days=1:40, J1=32:71, J5=42:81, J36=90:129) 

I would like to calculate linear regression equations for each of the columns when plotted against the independent variable "Days." I would then like to create vectors from the resulting co-efficients and R-values and add these as rows to my original dataframe.

This code is close (it works when you reverse "." and "Days") but it doesn't seem to work the way I have it. I also don't know the way to create a vector containing the r-values. Thanks for your help.

F1.Bare<-lm(.~ Days, data=F1)
vector1<-coefficients(F1.Bare)
F1.Bare<-rbind(F1, vector1)
user507
  • 223
  • 1
  • 6
  • 14
  • what is a *r-value*? Do you mean R²? – jogo Nov 06 '15 at 22:05
  • Yes that's correct...the co-efficient of determination. – user507 Nov 06 '15 at 23:20
  • you could try `fits <- lm(as.matrix(F1[-1]) ~ Days, data=F1)`, or `library(nlme); library(reshape2); dat <- melt(F1, id.vars = "Days"); fits2 <- lmList(value ~ Days | variable, data=dat); summary(fits2)$r.squared` will pull out all the R2 – Rorschach Nov 07 '15 at 00:14
  • This question really makes no sense on a dimensional basis. You are asking for some sort of association for what would be three numerical vectors (all of which can be seen to be 1.0 by inspection) with the three columns in F1. Using `rbind` on a dataframe in this manner is something that an Excel-user might imagine would be useful, but it is so dangerous to make such a hybrid data structure that I refuse to commit that sin. – IRTFM Nov 07 '15 at 01:30
  • Ok...will try to re-explain. The dimensions for each vector are not 1, the length instead matches the number of columns in the data frame. The reason is because I am trying to run a linear model for EACH "entire column" plotted against the "Days" column. For example, a lm of the J1 column plotted against the Days column would yield both a co-efficient and an R2 value. Then the same would be true for J5 when plotted against Days....and these values would be appended onto that same vector. If this is the case, I believe I should be able to add these vectors as rows? – user507 Nov 07 '15 at 14:19

0 Answers0