Hi!
I had a database with some variables in sequence. Let me say: TX01, TX02,
TX03 and TX04.
But I need to run some regressions changing the variables... so:
variable <- paste("TX0", 1:4, sep="")
for(i in 1:4){
test[i] <- lm(variable[i] ~ INCOME, data=database)
}
But doesn't work... lm tries to find a variable inside database named
variable[i] ...
Suggestions?
King regards!
Raphael Saldanha
Brasil
[[alternative HTML version deleted]]
?as.formula
copied from the help file:
## Create a formula for a model with a large number of variables:
xnam <- paste("x", 1:25, sep="")
(fmla <- as.formula(paste("y ~ ", paste(xnam, collapse=
"+"))))
--
David Winsemius
On Dec 1, 2008, at 7:45 PM, Raphael Saldanha wrote:
> Hi!
>
> I had a database with some variables in sequence. Let me say: TX01,
> TX02,
> TX03 and TX04.
>
> But I need to run some regressions changing the variables... so:
>
> variable <- paste("TX0", 1:4, sep="")
>
> for(i in 1:4){
> test[i] <- lm(variable[i] ~ INCOME, data=database)
> }
>
> But doesn't work... lm tries to find a variable inside database named
> variable[i] ...
>
> Suggestions?
>
>
> King regards!
>
> Raphael Saldanha
> Brasil
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hi Raphael,
If you truly only need to run 4 regressions you might be less confused
if you just do
test1 <- lm(TX01 ~ INCOME, data = database)
test2 <- lm(TX02 ~ INCOME, data = database)
test3 <- lm(TX03 ~ INCOME, data = database)
test4 <- lm(TX04 ~ INCOME, data = database)
If you need to do this 100 times it is not very practical. Here I'm
going to assume that your data are in a data frame with the first 100
columns being dependent variables and column 101 being the independent
variable. Your first option is to select the component from the lm
object you are interested in, let's assume the coefficients. Then
n <- 100
mat <- matrix(nrow=n,ncol=2)
for(i in 1:n){
mat[i,] <- lm(data[,i] ~ data[,101])$coef
}
The resulting matrix mat will hold the coefficients of the 100
regressions. Again, if you really need the whole lm object then you
could do the following
n <- 100
arr <- array(list(),c(1,n))
for(i in 1:n){
arr[[i]] <- lm(data[,i] ~ data[,101])
}
The resulting array will have the 100 lm objects, which you can then
examine.
Hope this helps,
Fernando
Raphael Saldanha wrote:> Hi!
>
> I had a database with some variables in sequence. Let me say: TX01, TX02,
> TX03 and TX04.
>
> But I need to run some regressions changing the variables... so:
>
> variable <- paste("TX0", 1:4, sep="")
>
> for(i in 1:4){
> test[i] <- lm(variable[i] ~ INCOME, data=database)
> }
>
> But doesn't work... lm tries to find a variable inside database named
> variable[i] ...
>
> Suggestions?
>
>
> King regards!
>
> Raphael Saldanha
> Brasil
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Fernando E. Miguez
Energy Biosciences Institute
http://netfiles.uiuc.edu/miguez/www/
Use this: lm(database[c(variable[i], "INCOME")]) On Mon, Dec 1, 2008 at 7:45 PM, Raphael Saldanha <saldanha.plangeo at gmail.com> wrote:> Hi! > > I had a database with some variables in sequence. Let me say: TX01, TX02, > TX03 and TX04. > > But I need to run some regressions changing the variables... so: > > variable <- paste("TX0", 1:4, sep="") > > for(i in 1:4){ > test[i] <- lm(variable[i] ~ INCOME, data=database) > } > > But doesn't work... lm tries to find a variable inside database named > variable[i] ... > > Suggestions? > > > King regards! > > Raphael Saldanha > Brasil > > [[alternative HTML version deleted]] > > _______________________________________________ > R-sig-Geo mailing list > R-sig-Geo at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/r-sig-geo >
How about doing something like:
x <- 1:100
y1 <- 1 + 2*x + rnorm(x)
y2 <- 2 + 3*x + rnorm(x)
lm(cbind(y1,y2) ~ x)
This is much faster.
Yours sincerely / Venlig hilsen
Frede Aakmann T?gersen
Statistician, M.Sc., Ph.D.
Modeling, Statistics and Risk Analysis
Wind and Site Competence Centre
Technology R&D
T: +45 9730 5135
M: +45 2547 6050
frtog at vestas.com
Company reg. name: Vestas Wind Systems A/S..
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
<http://www.vestas.com/legal/notice/owww.vestas.com/legal/notice>
If you have received this e-mail in error please contact the sender.
________________________________
From: r-sig-geo-bounces at stat.math.ethz.ch on behalf of Raphael Saldanha
Sent: Tue 02-12-2008 01:45
To: r-sig-geo at stat.math.ethz.ch; r-help at r-project.org
Cc: Ana Paula Sobral
Subject: [R-sig-Geo] Variables inside a for
Hi!
I had a database with some variables in sequence. Let me say: TX01, TX02,
TX03 and TX04.
But I need to run some regressions changing the variables... so:
variable <- paste("TX0", 1:4, sep="")
for(i in 1:4){
test[i] <- lm(variable[i] ~ INCOME, data=database)
}
But doesn't work... lm tries to find a variable inside database named
variable[i] ...
Suggestions?
King regards!
Raphael Saldanha
Brasil
[[alternative HTML version deleted]]
_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo