R regression on same line data

Asked

Viewed 52 times

1

I have the following data:

mes <- c("jan","fev","mar","abr","maio","jun","ago","set","out")
a <- c(32.3,32.7,32.6,33.1,33.0,33.5,33.4,33.4,34.9)
b <- c(19.2,19.2,19.6,19.7,19.7,19.9,20.0,20.0,20.4)
c <- c(14.7,15.0,15.6,16.2,16.4,17.0,17.7,18.3,19.1)
d <- c(24.2,24.3,24.7,25.0,25.5,26.4,26.7,27.1,27.6) 
temp <- data.frame(mes,a,b,c,d)
y <- c(1, 3, 5, 7)

would like to apply a regression lm(log(mes) ~ y)

but on the lines, and using a function lapply(), or Type Alo, to apply on all lines. one way I found to exemplify was this:

lm(log(temp[1,])~y)
lm(log(temp[2,])~y)
lm(log(temp[3,])~y)
lm(log(temp[4,])~y)

I don’t know if it’s possible.

the database I have is extensive, if it is possible to do this, as I would to run the data, row by row, saving the regression coefficients and the adjusted R2 of the model?

I thought I’d make a list, but I’m not sure how to do it, so if you could give me at least one north, I’d be grateful.

  • lm(log(mes) ~ y) It’ll be a mistake, I didn’t even try.

2 answers

3


It’s not as hard as that. In the solution below

  1. I calculate the logarithm of each column except the first one at the beginning.
  2. Applique (apply) the model lm(x ~ y) every row.
  3. And then there are several instructions to extract the various values one may want, such as the beta coefficients, the p-values, the coefficient of determination.

The code is really very simple, these are successive applications of *apply.

log_temp <- apply(temp[-1], 2, log)

model_list <- apply(log_temp, 1, function(x) lm(x ~ y))

coef_list <- t(sapply(model_list, coef))

model_smry <- lapply(model_list, summary)

R2_list <- sapply(model_smry, '[[', 'r.squared')
pval_list <- t(sapply(model_smry, function(LM){
  LM[['coefficients']][, 4]
}))
  • Thank you @Ruibarradas, you have some tips where to study and deepen the knowledge in r?

  • There are lots of documents on the official R page, The R Project for Statistical Computing. You can also find many others on the net. Or in bookstores. For example the collection of Springer among many others.

1

It is not possible to adjust the regression model the way you want, because it does not make sense. If you adjust a model for each baseline, vc will have a size 1 sample, and with that, vc will not have variability in the data to calculate the standard errors of the model estimators/variance.

I suggest reading some book of the type Linear Regression Analysis in R (has several), so that you continue to model the data.

Still, "y" should be in the same data.frame for better organization of the database.

  • In fact, AP can do the regression you want. Discounted the column of the month, each row of the data frame temp has 4 comments, which is the same number of elements in y.

  • @Guilherme, I understand, but it’s like @Marcusnunes said. I don’t think I expressed myself well in the question. My database has more observations, I’ve simplified for a,b,c,d, and y with 4 comments as well. @Marcusnunes what would be AP?

  • AP = Author of the question

  • Beauty! At the time, I had read on the bus, and had never come across this data entry, so I thought it would not be possible. But according to Rui’s code, and given the form that the data enter, each baseline acts as the "traditional vector", being "y" my covariable. I understand. I will reconstruct my answer in the future in a way that I think is more logical.. Thank you!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.