0

I am a R-newcomer and currently trying to create 3 vectors consisting of the beta values of a multiple regression model. I have three predictors Age, Fitness and sleep and a dependent variable RT.

For each participant I have a excel table with 10 measurements at different time points. For each participant 1 to 20 I would like to save the betas of the multiple regression in the respective vector Age_betas, Fitness_betas or Sleep_betas.

In case I try my model for only one participant e.g.

model<-lm(scale(RT)~scale(Age)+scale(Fitness)+scale(Sleep), data=subset(Subjects1_5, Subject==1)) 

it works perfectly fine. Could someone help me why my loop won't work?

    Fun<-function()
  {  
  Age_betas<-c()
  Fitness_betas<-c()
  Sleep_betas<-c()

  for (counter in 1:5)

    {model<-lm(scale(RT)~scale(Age)+scale(Fitness)+scale(Sleep), data=subset(Subjects1_5, Dummy==1 & Subject==i))

    Age_betas <- c(Age_betas, model[["coefficients"]][["scale(Age)"]])

    Fitness_betas <- c(Fitness_betas, model[["coefficients"]][["scale(Fitness)"]])

    Sleep_betas <- c(Sleep_betas, model[["coefficients"]][["scale(Sleep)"]])} 
  }

There is no error, but the script doesn't do anything except adding the function fun.

Fun<-function() + {
+ Age_betas<-c() + Fitness_betas<-c() + Sleep_betas<-c() +
+ for (counter in 1:5) +
+ {model<-lm(scale(RT)~scale(Age)+scale(Fitness)+scale(Sleep), data=subset(Subjects1_5, Dummy==1 & Subject==i)) +
+ Age_betas <- c(Age_betas, model[["coefficients"]][["scale(Age)"]]) +
+ Fitness_betas <- c(Fitness_betas, model[["coefficients"]][["scale(Fitness)"]]) +
+ Sleep_betas <- c(Sleep_betas, model[["coefficients"]][["scale(Sleep)"]])} + }

Max
  • 9
  • 2
  • Welcome to SO. Please provide a minimum reproducible example (https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with some test data. It would also be helpful if you were explaining what exactly does not work. Is there an error message? – Ben Nov 28 '18 at 09:25
  • What error do you get? – ebeneditos Nov 28 '18 at 09:51
  • Unfortunately, I haven't yet found out how to upload data. So here the link to the file for 5 subjects [link](https://docs.google.com/spreadsheets/d/1dmcUntSn1x5e2VcWk6jCKwa7TdRGT3TsG1xNyh9-Hs4/edit?usp=sharing) – Max Nov 28 '18 at 09:51
  • Are you aware of the difference between defining a function and calling a function? – Roland Nov 28 '18 at 09:57
  • No. I really don't have an idea where things are going wrong and currently trying to read up on this – Max Nov 28 '18 at 10:03
  • There are further issues, but your main problem is that you are defining a function but not calling it. You need to do `Fun()`. But first, you have to add a return value to your function definition. – Roland Nov 28 '18 at 10:05

2 Answers2

0

Found out a way to solve the problem:

 age_betas<-c()
    fitness_betas<-c()
    sleep_betas<-c()

    for (i in 1:5){
      model<-lm(scale(RT)~scale(Age)+scale(Fitness)+scale(Sleep), data=subset(Subjects1_5, Dummy==1 & Subject==i))

      age_betas <- c(age_betas, model[["coefficients"]][["scale(Age)"]])
      fitness_betas <- c(fitness_betas, model[["coefficients"]][["scale(Fitness)"]])
      sleep_betas <- c(sleep_betas, model[["coefficients"]][["scale(Sleep)"]])
    }
Max
  • 9
  • 2
0

I believe what you desire your function to finally do is return the Age_betas, Fitness_betas and the Sleep_betas in their respective vectors. However, you haven't included in your user function the return() for an output. Also note that multi-argument returns are not permitted for the function; return().

What to do:

  1. Create an empty list beta; this will be the list of lists where we store the beta vectors
  2. Add the return function to output the beta list of lists
  3. Then call the function Fun user-defined function to display the beta vectors.
Fun <- function(){
  beta <- list()
  
  for (counter in 1:5){
    model<-lm(scale(RT)~scale(Age)+scale(Fitness)+scale(Sleep), data=subset(Subjects1_5, Dummy==1 & Subject==i))
    beta[["Age_betas"]] = c(beta[["Age_betas"]], model[["coefficients"]][["scale(Age)"]])
    beta[["Fitness_betas"]] = c(beta[["Fitness_betas"]], model[["coefficients"]][["scale(Fitness)"]])
    beta[["Sleep_betas"]] = c(beta[["Sleep_betas"]], model[["coefficients"]][["scale(Sleep)"]])
  }
  return(beta)
}

# Call Function:
Fun()

The output:

# $Age_betas
# [1] -0.3970718 -0.3970718 -0.3970718 -0.3970718 -0.3970718

# $Fitness_betas
# [1] -0.004210636 -0.004210636 -0.004210636 -0.004210636 -0.004210636

# $Sleep_betas
# [1] -0.07401978 -0.07401978 -0.07401978 -0.07401978 -0.07401978
Musebe Ivan
  • 160
  • 7