-3
$\begingroup$

this is, I think, an easy question... I did a regression analysis in R, where I wanted to check the fit of my data to a specific formula I provided... I got this to work, I see the graph and the line showing the fit of the data, but I also would like to get actual regression value obtained from this calculation, how do I do this? and How do I get the actual equation printed in the graph?


Hi thanks a lot for the replies.

The example was very useful, but I still didnt obtain the Regression value, so I still dont know how well my data fits the equation... below Im pasting the code I used

# import the input data file "LD251-chilR.txt", for the input; 
# the first column is pairwise distance and the second is LD estimate, 
# need to put the file in the same folder with R program
CT251chil<-read.table("LD251-chilR.txt", sep="\t", dec=",", header=TRUE) 

# your LD estimate (r2 in this case which located in column 2)
r2=CT251chil[,2] 

# run nls function for getting rho estimate
nls(r2~1/(1+rho*CT251chil[,1]), start=list(rho=0.3)) 

# getting this rho estimate after running nls function in R
rho=0.02206872 

#sort the data, no need if your data already sorted
dist<-sort(CT251chil[,1]) 

# put parameters in the equation as shown in my MBE paper
eq <- (((10+rho*dist)/((2+rho*dist)*(11+rho*dist)))*(1+((3+rho*dist)*(12+12*rho*dist+(rho*dist)^2)/(46*(2+rho*dist)*(11+rho*dist))))) 

# plotting the graph between LD estimate and distance
plot(CT251chil[,1],CT251chil[,2], col="black", pch=20, 
     ylab=expression(R^2), xlab="Pairwise distance", 
     main="CT251-chilense", las=1) 

# getting regression line
lines(dist,eq, col="black",lwd=2,lty=1) 

After this point I get a graph with a line, all good. But so far I dont know how to output the actual equation on the graph get the value of R.

thank you for any further help!

$\endgroup$

closed as off-topic by whuber Jun 18 '16 at 16:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – whuber
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ Yes, this is straightforward, but it depends on what you mean "actual regression value". Also, if you post the code that produced your graph it will be easier for someone to give you a pointer on how to add an eqquation to it; but as a starter try ?plotmath $\endgroup$ – Peter Ellis Mar 16 '12 at 1:07
  • $\begingroup$ Or maybe ?predict if by "actual regression value" you mean the value that the regression would predict. $\endgroup$ – Brian Diggs Mar 16 '12 at 4:16
  • 2
    $\begingroup$ @Karl Please register your account. $\endgroup$ – user88 Mar 16 '12 at 18:50
  • $\begingroup$ Karl, I've merged your two accounts. As suggested two days ago, please register once and for all. $\endgroup$ – chl Mar 19 '12 at 15:26
2
$\begingroup$

Do you mean the coefficients?

Extending @MånsT example.

x<-1:5
y<-2*x+rnorm(5)

# Regression:
m<-lm(y~x)
names(m)
m$coefficients

> m$coefficients
(Intercept)           x 
 -0.7811257   2.2952913
$\endgroup$
1
$\begingroup$

Here's an example of how to get the results of the calculation and add the equation of the fitted line.

x<-1:5
y<-2*x+rnorm(5)
plot(x, y)

# Regression:
m<-lm(y~x)
abline(coef(m), col=2)

# Summary of calculated values:
summary(m)

# Add equation of fitted line to the plot using the text command
# Regression coefficients rounded to three decimal places
text(2.5,8,paste("y=",round(coef(m)[1],3),"+",round(coef(m)[2],3),"x",sep=""))
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.