Reputation: 2022
First of all I want to point out that I'm not very familiar with R, so sorry if one of the following questions is clear.
My motivation is to write a simple R-script, which should contain:
now here are my questions:
lm
, like fit<-lm(Y~X+Z,data=database)
this gives a nice ouput. What I really want is to save the coefficients of the model in a vector. How can I do this? Here would it be a 3-dimensional vector (intercept, a, b)
. EDIT I've tried coefficient<-coefficient(fit)
. This does not work! coefficient
is not a numerical vector. There are also the name, i.e. intercept and the value below for the first element of it.print(....)
?I'm very thankful for your help and Hopefully I considered all rules and conventions in this forum, since this is my first question. If not, I'm very sorry.
Upvotes: 0
Views: 528
Reputation: 51670
If I wrote the R script, then I have to load it with source(name.R), right? Must be there an additional command to execute the script?
Not if your script directly invokes the commands
For instance if name.R contains
a <- 1:10
plot(a, a^2, t="l")
Then source("name.R") will directly generate a plot
However, if name.R contains
myfunction <- function()
{
a <- 1:10
plot(a, a^2, t="l")
}
Then sourcing it will only load the function. You will then have to invoke myfunction()
to get the plot.
Suppose I did my regression with lm, like fit<-lm(Y~X+Z,data=database) this gives a nice ouput. What I really want is to save the coefficients of the model in a vector. How can I do this? Here would it be a 3-dimensional vector (intercept, a, b)
If I want to print out the coefficients and some calculations at the very end of the script, how do I do this? Just write print(....)?
print(coef(fit))
will give you what you need (you can store them in an array with model.coef <- coef(fit)
)
Also, it can be interesting to run
summary(fit)
See ?coef
and ?summary
for more info
Upvotes: 2