For Loop Problem with a linear regression function

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

For Loop Problem with a linear regression function

Eddie_666
This post was updated on .
I have to generate m slope-coefficients for a linear regression model, I am using a For loop for this.

But, I have no idea why my the arguments inside my For loop are just not running, R-Studio just does not run that part.

This is my code:

myfun<-function(m){
  x<-c(1,2,3,4,5,6,7,8,9,10)
  for(i in 1:i){
  e<-rnorm(10)
  y<-1+0.3*x+e
  model<-lm(y~x)
  b1<-2
  assign(paste("b1",i,sep=""),model$coefficients[2])}
  e
  b1
}
myfun(7)

And when I run it, it only gives me the value of b1 and it gives me that b1 = 2 which is a wrong value, I want it to be equal to model$coefficients[2] and doesn't give me the value of e.
Reply | Threaded
Open this post in threaded view
|

Re: For Loop Problem with a linear regression function

Patrick_Schn01
I did not take the time to try and figure out what you are doing, but this is obviously not going to work:

for(i in 1:i){

should be:

 for(i in 1:length(x)){

or something else, but not for(i in 1:i)




Eddie_666 wrote
I have to generate m slope-coefficients for a linear regression model, I am using a For loop for this.

But, I have no idea why my the arguments inside my For loop are just not running, R-Studio just does not run that part.

This is my code:

myfun<-function(m){
  x<-c(1,2,3,4,5,6,7,8,9,10)
  for(i in 1:i){
  e<-rnorm(10)
  y<-1+0.3*x+e
  model<-lm(y~x)
  b1<-2
  assign(paste("b1",i,sep=""),model$coefficients[2])}
  e
  b1
}
myfun(7)

And when I run it, it only gives me the value of b1 and it gives me that b1 = 2 which is a wrong value, I want it to be equal to model$coefficients[2] and doesn't give me the value of e.
Reply | Threaded
Open this post in threaded view
|

Re: For Loop Problem with a linear regression function

Eddie_666
Yes, my bad. It is supposed to be: for(i in 1:m)

But I believe the problem resides in another reason. Because, even though I pick a number it still won't work.

For example: for(i in 1:7)

It still doesn't give me the value of e
Reply | Threaded
Open this post in threaded view
|

Re: For Loop Problem with a linear regression function

Patrick_Schn01
I still have no idea what you are trying to accomplish, so you might want to annotate your code a bit if you want useful help.

Does this give you what you want?


myfun<-function(m){
  x<-c(1,2,3,4,5,6,7,8,9,10)
out.i <- vector("list", length=m)
  for(i in 1:m){
  e<-rnorm(10)
  y<-1 + 0.3*x + e
  model<-lm(y~x)
out.i[[i]] <-  model$coefficients[2]
 
 }
out <- unlist(out.i)
names(out) <- paste("b1", 1:m,sep="")
return(out)
}
myfun(7)



Eddie_666 wrote
Yes, my bad. It is supposed to be: for(i in 1:m)

But I believe the problem resides in another reason. Because, even though I pick a number it still won't work.

For example: for(i in 1:7)

It still doesn't give me the value of e