Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)
I am looking for just the value of the B1(newx) linear model coefficient, not the name. I just want the 0.5 value. I do not want the name "newx".
newx <- c(0.5,1.5.2.5)newy <- c(2,3,4)out <- lm(newy ~ newx)
out looks like:
Call:lm(formula = newy ~ newx)Coefficients:(Intercept)         newx         1.5         1.0  
I arrived here. But now I am stuck.
out$coefficients["newx"]newx 1.0 

1 Answer

0 votes
by

To extract a single element, you can use the [[ instead of [.

In your case:

newx <- c(0.5,1.5,2.5)

newy <- c(2,3,4)

out <- lm(newy ~ newx)

 coefficients(out)[["newx"]]

[1] 1

You can also use the unname() function as follows:

 unname(coefficients(out)[c("newx", "(Intercept)")])

[1] 1.0 1.5

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...