2014-06-05 77 views
2

我有下面的一组xy值:如何计算R中线性模型的'确定系数'?

x = c(1:150) 
y = x^-.5 * 155 + (runif(length(x), min=-3, max=3)) 

和运行上的数据进行线性回归:

plot(x, y, log="xy", cex=.5) 

model = lm(log(y) ~ log(x)) 
model 

现在我想拥有的品质衡量回归,并被告知人们通常使用R^2('决心系数'),这应该接近于1。

如何轻松计算R中的值?我已经看到残差等已经计算出来了。

+2

尝试'印刷的summary(model)刚刚过去的部分摘要(模型)'。 – Victorp

回答

3

使用summary(model)将详细输出打印到控制台。您还可以使用str来探索对象的结构,例如str(summary(model))当您可以使用$提取部分输出时,这非常有用。

result <- summary(model) # summary of the model 
str(result)    # see structure of the summary 
result$r.squared   # extract R squared (coefficient of determination) 

result同时包含,R.squared和调整的R平方,参见例如以下输出

Residual standard error: 0.09178 on 148 degrees of freedom 
Multiple R-squared: 0.9643, Adjusted R-squared: 0.964 
F-statistic: 3992 on 1 and 148 DF, p-value: < 2.2e-16 

该输出是在控制台

相关问题