当我尝试在R中定义我的线性模型时,如下所示:
lm1 <- lm(predictorvariable ~ x1+x2+x3, data=dataframe.df)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
Run Code Online (Sandbox Code Playgroud)
有没有办法忽略它或修复它?有些变量是因素,有些则不是.
我正在尝试运行混合效果模型,F2_difference该模型预测其余列作为预测变量,但我收到一条错误消息
固定效应模型矩阵排名不足,因此下降7列/系数.
从这个链接,固定效果模型是排名不足,我想我应该findLinearCombos在R包中使用caret.但是,当我尝试时findLinearCombos(data.df),它给了我错误信息
qr.default(object)中的错误:外部函数调用中的NA/NaN/Inf(arg 1)另外:警告消息:在qr.default(object)中:强制引入的NAs
我的数据没有任何NA - 可能导致这种情况的原因是什么?(对不起,如果答案很明显 - 我是R的新手).
我的所有数据都是除了我试图预测的数值之外的因素.这是我的数据的一小部分样本.
sex <- c("f", "m", "f", "m")
nasal <- c("TRUE", "TRUE", "FALSE", "FALSE")
vowelLabel <- c("a", "e", "i", "o")
speaker <- c("Jim", "John", "Ben", "Sally")
word_1 <- c("going", "back", "bag", "back")
type <- c("coronal", "coronal", "labial", "velar")
F2_difference <- c(345.6, -765.8, 800, 900.5)
data.df <- data.frame(sex, nasal, vowelLabel, speaker,
word_1, type, F2_difference
stringsAsFactors = TRUE)
Run Code Online (Sandbox Code Playgroud)
编辑:这是一些更多的代码,如果它有帮助.
formula <- F2_difference ~ …Run Code Online (Sandbox Code Playgroud) 我有一个关于R.的问题
我正在使用一个名为levene.test的测试来测试方差的同质性.
我知道你需要一个至少有两个级别的因子变量才能使它工作.从我看来,我确实至少有两个级别用于我正在使用的因子变量.但不知怎的,我不断得到错误:
> nocorlevene <- levene.test(geno1rs11809462$SIF1, geno1rs11809462$k, correction.method = "correction.factor")
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
Run Code Online (Sandbox Code Playgroud)
我甚至尝试从二项分布生成变量:
k<-rbinom(1304, 1, 0.5)
Run Code Online (Sandbox Code Playgroud)
然后使用它作为一个因素,但仍然无法正常工作.
最后,我创建了一个3级变量:
k<-sample(c(1,0,2), 1304, replace=T)
Run Code Online (Sandbox Code Playgroud)
但有些人仍然没有工作并得到同样的错误:
nocorlevene < - levene.test(geno1rs11809462 $ SIF1,geno1rs11809462 $ k,correction.method ="zero.removal")
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
Run Code Online (Sandbox Code Playgroud)
这是数据中变量类型的输出:
> str(geno1rs11809462)
'data.frame': 1304 obs. of 16 …Run Code Online (Sandbox Code Playgroud) 我有以下代码用于使用optim()来查找beta0和beta1来最小化偏差之和但我收到以下错误我不知道我做错了什么:
sum.abs.dev<-function(beta=c(beta0,beta1),a,b)
{
total<-0
n<-length(b)
for (i in 1:n)
{
total <- total + (b[i]-beta[1]-beta[2]*a[i])
}
return(total)
}
tlad <- function(y = "farm", x = "land", data="FarmLandArea.csv")
{
dat <- read.csv(data)
#fit<-lm(dat$farm~dat$land)
fit<-lm(y~x,data=dat)
beta.out=optim(fit$coefficients,sum.abs.dev)
return(beta.out)
}
Run Code Online (Sandbox Code Playgroud)
这是错误和警告:
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels In addition: Warning message:
In model.response(mf, "numeric") : NAs introduced by coercion
Run Code Online (Sandbox Code Playgroud)

我正在使用R插入包来生成模型.我在预处理过程中使用PCA来降低维数,然后尝试生成逻辑回归模型.
我收到这个错误:
误差在contrasts<-(*tmp*,值= contr.funs [1 + ISOF [NN]]):对比度可以只应用于因素有2倍或更多的水平
credit <- read.csv('~Loans Question/RequiredAttributesWithLoanStatus.csv')
credit$LoanStatus <- as.factor(credit$LoanStatus)
str(credit)
'data.frame': 8580 obs. of 45 variables:
$ ListingCategory : int 1 7 3 1 1 7 1 1 1 1 ...
$ IncomeRange : int 3 4 6 4 4 3 3 4 3 3 ...
$ StatedMonthlyIncome : num 2583 4326 10500 4167 5667 ...
$ IncomeVerifiable : logi TRUE TRUE TRUE FALSE TRUE TRUE ...
$ DTIwProsperLoan : …Run Code Online (Sandbox Code Playgroud) 我在R中看到了一些数据.一个标题为"Height"的特定列包含几行NA.
我期待我的数据框的子集,以便从我的分析中排除高于某个值的所有高度.
df2 <- subset ( df1 , Height < 40 )
Run Code Online (Sandbox Code Playgroud)
但是,每当我执行此操作时,R会自动删除包含高度NA值的所有行.我不想要这个.我试过为na.rm包含参数
f1 <- function ( x , na.rm = FALSE ) {
df2 <- subset ( x , Height < 40 )
}
f1 ( df1 , na.rm = FALSE )
Run Code Online (Sandbox Code Playgroud)
但这似乎没有做任何事情; NA的行仍然从我的数据框中消失.有没有一种方法可以对我的数据进行子集化,而不会丢失NA行?
误差在contrasts<-(*tmp*,值= contr.funs [1 + ISOF [NN]]):对比度可以只应用于因素有2倍或更多的水平
每当我尝试在调查包中使用svychisq函数时,我都会收到此错误.但是当我使用svytable函数时,该函数可以正常工作.该错误涉及具有2级或更多级别的因子--DIED变量是具有2级,0和1的因子.
> svytable(~COHORT+DIED, design=df_srvy)
DIED
COHORT 0 1
1997 26726.584 1647.118
2000 26958.912 1628.692
2003 30248.533 1599.094
2006 36602.173 1586.526
2009 44004.732 2531.597
2012 56037.874 2766.386
> svychisq(~COHORT+DIED, design=df_srvy)
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
Run Code Online (Sandbox Code Playgroud)
编辑:
这是问题的一个小子集示例
sample <- structure(list(DISCWT = c(1.36973, 1.4144, 1.41222, 1.41222,
1.4144, 1.4144, 1.41222, 1.41222, 1.4144, 1.41222, 1.41222, 1.41222,
1.41222, 1.4144, …Run Code Online (Sandbox Code Playgroud) 我发现包中predict的svyglm对象和对象有一些奇怪的行为survey。如果您的 newdata 中predict的因子/字符具有一个级别,则会输出错误:
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
Run Code Online (Sandbox Code Playgroud)
如果我将一个单级变量作为模型的预测变量,这个错误是有道理的,但对于 newdata 我没有看到问题。
定期glm这工作正常。
雷:
library(survey)
data(api)
dstrat<-svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
svymodel <- svyglm(api00~sch.wide,design=dstrat)
# errors
predict(svymodel, data.frame(sch.wide=rep("No",10)))
regmodel <- glm(api00~sch.wide,data=apistrat)
# works
predict(regmodel,data.frame(sch.wide=rep("No",10)))
Run Code Online (Sandbox Code Playgroud)
我发现如果我破解因子的水平它会起作用,但这应该不是必需的:
svymodel <- svyglm(api00~sch.wide,design=dstrat)
predict(svymodel, data.frame(sch.wide=factor(rep("No",10),
levels = c("No","random phrase"))))
Run Code Online (Sandbox Code Playgroud)
我是不是误解了什么,或者这是survey包裹的问题?
当我们使用传统的逻辑回归并在R中进行预测时,例如:
library(dplyr)
n = 300
xx<-c("r1","r2","r3","r4","r5")
xxx<-c("e1","e2","e3")
p=0.3
df1 <- data_frame(
xx1 = runif(n, min = 0, max = 10),
xx2 = runif(n, min = 0, max = 10),
xx3 = runif(n, min = 0, max = 10),
School = factor(sample(xxx, n,re=TRUE)),
Rank = factor(sample(xx, n,re=TRUE)),
yx = as.factor(rbinom(n, size = 1, prob = p))
)
df1
mm<-glm(yx ~ xx1 + xx2 + xx3 + School + Rank,binomial,df1)
n11 = data.frame(School="e3",Rank="r2",xx1=8.58,xx2=8.75,xx3=7.92)
Run Code Online (Sandbox Code Playgroud)
我们用:
predict(mm, n11, type="response") #No meu caso especifico
Run Code Online (Sandbox Code Playgroud)
你预测(mm,n11) …
给出以下数据框:
structure(list(`-5` = c(0, 1, 0, 0, 9, 22), `-4` = c(1, 3, 0,
0, 1, 17), `-3` = c(1, 3, 0, 0, 0, 12), `-2` = c(1, 3, 0, 0,
2, 10), `-1` = c(0, 0, 0, 4, 3, 9), `0` = c(0, 1, 0, 2, 2, 21
), `1` = c(0, 1, 1, 7, 1, 21), `2` = c(1, 0, 1, 2, 1, 10), `3` = c(0,
9, 0, 6, 1, 12), `4` = c(0, 2, 0, 5, …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作和测试线性模型如下:
lm_model <- lm(Purchase ~., data = train)
lm_prediction <- predict(lm_model, test)
Run Code Online (Sandbox Code Playgroud)
这会导致以下错误,表明该Product_Category_1列具有test数据框中存在的值,但不存在train数据框中的值):
因子Product_Category_1具有新的级别7,9,14,16,17,18
但是,如果我检查这些,他们肯定会出现在两个数据框中:
> nrow(subset(train, Product_Category_1 == "7"))
[1] 2923
> nrow(subset(test, Product_Category_1 == "7"))
[1] 745
> nrow(subset(train, Product_Category_1 == "9"))
[1] 312
> nrow(subset(test, Product_Category_1 == "9"))
[1] 92
Run Code Online (Sandbox Code Playgroud)
同时显示表格train并test显示它们具有相同的因素:
> table(train$Product_Category_1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
110820 18818 15820 9265 118955 16159 …Run Code Online (Sandbox Code Playgroud) r ×11
regression ×4
lm ×2
lme4 ×2
prediction ×2
survey ×2
csv ×1
database ×1
dataframe ×1
function ×1
glm ×1
mixed-models ×1
na ×1
optimization ×1
r-caret ×1
statistics ×1
subset ×1
variables ×1