2014-06-05 79 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 

該輸出是在控制檯

相關問題