有谁知道如何解决这个问题附着在两行代码?我相信as.matrix会创建一个矩阵,X
,然后使用X %*% X
,t(X)
和solve(X)
来获得答案。但是,它似乎并没有工作。任何答案都会有所帮助,谢谢。
Q
计算系数
-3
A
回答
1
我会建议使用read.csv
代替read.table
这将是有益的,你走过去的两个功能在这个线程的区别:基于@kon_u的答案read.csv vs. read.table
df <- read.csv("http://pengstats.macssa.com/download/rcc/lmdata.csv")
model1 <- lm(y ~ x1 + x2, data = df)
coefficients(model1) # get the coefficients of your regression model1
summary(model1) # get the summary of model1
0
,这里是一个用手做的例子:
df <- read.csv("http://pengstats.macssa.com/download/rcc/lmdata.csv")
model1 <- lm(y ~ x1 + x2, data = df)
coefficients(model1) # get the coefficients of your regression model1
summary(model1) # get the summary of model1
### Based on the formula
X <- cbind(1, df$x1, df$x2) # the column of 1 is to consider the intercept
Y <- df$y
bhat <- solve(t(X) %*% X) %*% t(X) %*% Y # coefficients
bhat # Note that we got the same coefficients with the lm function
+0
请注意,此公式允许您也计算多元线性回归的系数。 – ANG
相关问题
- 1. 计算FFT相关系数
- 2. 计算一系列
- 3. 与GGPLOT2计算相关系数R中
- 4. 计算机代数系统介绍?
- 5. 在Python中计算确定系数
- 6. scipy计算的多项系数
- 7. 使用PostgreSQL计算相关系数?
- 8. 需要使用MATLAB计算Pearson系数
- 9. 从频率响应计算FIR系数
- 10. Pearson相关系数计算阵列
- 11. 如何计算rsa私钥的系数?
- 12. 构建计算机代数系统
- 13. 并行计算二项式系数
- 14. Javascript积分计算系统
- 15. 计算在android系统
- 16. 计算一系列值
- 17. 元组关系计算
- 18. ř计算一系列
- 19. 计算系列的总和
- 20. 计算一个系列
- 21. EmberJS - 计算异步关系
- 22. DBMS:关系代数执行计划成本计算
- 23. exp()函数的数值计算中的系数
- 24. 计算质数
- 25. 计算数量
- 26. 总数计算
- 27. 计算数量
- 28. 计算天数
- 29. 计算分数
- 30. 计算对数
请告诉我们你到目前为止试过的东西。这将有助于您生成[可重现的示例](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) –