0

Hey guys I'm having trouble with R and which codes to use (im a newbie):

So I have some data, I've performed a simple linear regression (SLR), however I found that this doesn't give me the relationship I want.

So I performed another SLR analysis using the log (base 'e') of both the variables and it seems to be a better fit.

My question is that what codes do I use to include plot by log SLR onto my original data (into a smooth curve) i.e. how do I fit the new data, with a log scale, onto my original data?

From what I understand I use the following:

1) plot(x,y) <-- Original data

2) lines(fitted(exp(____))) <-- Not sure about what to do here

If you guys could help me that would be great!

Vincent Bonhomme
  • 7,235
  • 2
  • 27
  • 38
yomamg
  • 9
  • 1
  • Have you had a look to `?lm`, `?abline` and their examples? – Vincent Bonhomme Apr 02 '16 at 05:50
  • Yeah I have. The questions requires me to draw a smooth curve from my log analysis on a plot with the "original data". I'm not sure if I am explaining the question clearly. I can copy part of the question: Plot YYYY against XXXX, and include the SLR model from part (d) as a line on this plot. Now include the two alternative models from part (e) [THIS IS THE LOG ANALYSIS PART] as smooth “curves” on the plot, using different line types for the three models – yomamg Apr 02 '16 at 07:40

1 Answers1

1

Welcome to StackOverflow!

To get better help around here I recommend you to read and follow How to make a great R reproducible example? the next time you have a question. This way you can increase the quality of your question significantly. And while creating a reproducible example one often solves the problem on its own.

Anyways, I created a sample model that I assume fits your situation quite well:

sampledata <- data.frame(x=1:50, y=1.1^(1:50+rnorm(50)))
model <- lm(log(y) ~ x, sampledata)
summary(model)

Plotting the model using R's default tools is now easy:

plot(sampledata$x, sampledata$y)
lines(sampledata$x, exp(predict(model)))
Community
  • 1
  • 1
Thilo
  • 8,827
  • 2
  • 35
  • 56