Use the lm command to create the linear-log model in R. The linear log function takes the form Y=a+blog(x)
> #generating data for the problem
> n=500
> x <- 1:n
> set.seed(10)
> y <- 1*log(x)-6+rnorm(n)
>
> #plotting the data
> plot(y~x)
>
> #fitting log model
>
> fit <- lm(y~log(x))
> #Summary of the model
> summary(fit)
Call:
lm(formula = y ~ log(x))
Residuals:
Min 1Q Median 3Q Max
-3.06157 -0.69437 -0.00174 0.76330 2.63033
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -6.4699 0.2471 -26.19 <2e-16 ***
log(x) 1.0879 0.0465 23.39 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.014 on 498 degrees of freedom
Multiple R-squared: 0.5236, Adjusted R-squared: 0.5226
F-statistic: 547.3 on 1 and 498 DF, p-value: < 2.2e-16
>
> coef(fit)
(Intercept) log(x)
-6.469869 1.087886
>
> #plotting
> x=seq(from=1,to=n,length.out=1000)
> y=predict(fit,newdata=list(x=seq(from=1,to=n,length.out=1000)),
+ interval="confidence")
> matlines(x,y,lwd=2)
Output:
Get Answers For Free
Most questions answered within 1 hours.