初学R语言,用lm跑回归时出错,求助

Python016

初学R语言,用lm跑回归时出错,求助,第1张

方法1:attach(data)regression1<- lm(AmountSpent~Salary,data=data);最后detach(data)

方法2:regression1<- lm(data$AmountSpent~data$Salary)

方法3:with函数

模型拟合

对于人口模型可以采用Logistic增长函数形式,它考虑了初期的指数增长以及总资源的限制。其函数形式如下。

首先载入car包以便读取数据,然后使用nls函数进行建模,其中theta1、theta2、theta3表示三个待估计参数,start设置了参数初始值,设定trace为真以显示迭代过程。nls函数默认采用Gauss-Newton方法寻找极值,迭代过程中第一列为RSS值,后面三列是各参数估计值。然后用summary返回回归结果。

library(car)

pop.mod1 <- nls(population ~ theta1/(1+exp(-(theta2+theta3*year))),start=list(theta1 = 400, theta2 = -49, theta3 = 0.025), data=USPop, trace=T)

summary(pop.mod)

在上面的回归过程中我们直接指定参数初始值,另一种方法是采用搜索策略,首先确定参数取值范围,然后利用nls2包的暴力方法来得到最优参数。但这种方法相当费时。

还有一种更为简便的方法就是采用内置自启动模型(self-starting Models),此时我们只需要指定函数形式,而不需要指定参数初始值。本例的logistic函数所对应的selfstarting函数名为SSlogis

pop.mod2 <- nls(population ~ SSlogis(year,phi1,phi2,phi3),data=USPop)

二、判断拟合效果

非线性回归模型建立后需要判断拟合效果,因为有时候参数最优化过程会捕捉到局部极值点而非全局极值点。最直观的方法是在原始数据点上绘制拟合曲线。

library(ggplot2)

p <- ggplot(USPop,aes(year, population))

百度知道

r语言找不到对象geneexp

查看全部1个回答

百度网友0f1c54f9b

超过127用户采纳过TA的回答

关注

成为第12位粉丝

r语言找不到对象geneexp,原因和解决方法如下。cor.test(x, ...)

## Default S3 method:

cor.test(x, y,

alternative = c("two.sided", "less", "greater"),

method = c("pearson", "kendall", "spearman"),

exact = NULL, conf.level = 0.95, continuity = FALSE, ...)

## S3 method for class 'formula'

cor.test(formula, data, subset, na.action, ...)

根本没有

cor.test(first,second,data= weightBJ_data)

这种调用方式,所以不识别对象first,second

你可以用

attach(weightBJ_data)

cor.test(first,second)

#或者

cor.test(weightBJ_data$first,weightBJ_data$second)

#或者

cor.test(weightBJ_data[,1],weightBJ_data[,2])

你用过attach(weightBJ_data)之后

first才能识别,但应该是没有逗号的。

first[2]